《笨办法学Python》笔记33-----一个项目骨架

骨架目录

为什么要建立这么个骨架?

建立一个项目的骨架目录就如同代码风格,统一规范的项目骨架目录应当是能提高项目的可读性的,进而为后来人提供快速方便的项目维护参考,降低项目维护的成本。

基本的框架包括项目布局,自动化测试,模块,安装脚本。

ubuntu下新建目录和文件的命令分别是:

建立目录 mkdir

建立文件 touch

按以下结构建立项目骨架

.
├── bin
├── docs
├── NAME
│   └── __init__.py
├── setup.py
└── tests
    ├── __init__.py
    └── NAME_tests.py

4 directories, 4 files

骨架下的这些文件和文件夹各有什么作用?

bin:存放可执行文件

docs:存放项目文档

NAME:是与项目名称一致的文件夹,可用大小写与项目根目录名区分开来。主要用来存放项目源代码文件,包括模块、包等。

setup.py:是项目安装、部署、打包的脚本

tests:主要存放项目测试代码

可参考
设计清晰的python项目目录结构
开源一个python项目的正确方式

安装软件包

pip

sudo apt intall python-pip

pip是一个以Python计算机程序语言写成的软件包管理系统,他可以安装和管理软件包

pip的其中一个主要特点就是其方便使用的命令列界面,这让使用者可以透过以下的一句文字命令来轻易地安装Python软件包:

pip install some-package-name

此外,使用者也可以轻易地透过以下的命令来移除软件包:

pip uninstall some-package-name

pip也拥有一个透过“需求”档案来管理软件包和其相应版本数目的完整列表之功能,这容许一个完整软件包组合可以在另一个环境(如另一部电脑)或虚拟化环境中进行有效率的重新创造。这个功能可以透过一个已正确进行格式化的文字档案和以下的命令来完成:

pip install -r requirements.txt

distribute

sudo apt intall python-distribute

这又是一个包管理工具。官方网站介绍说

`Distribute` is a now deprecated fork of the `Setuptools` project.

Distribute was intended to replace Setuptools as the standard method
for working with Python module distributions. The code has since been merged
back into the parent project as is being maintained by the community at large.

`Distribute` is now being maintained as a branch in the `Setuptools repository

作为一个初学者,可能已经被众多包管理工具搞糊涂了,以下是一系列文章,可以参考

python包管理工具解惑
Python 的包管理工具 distribute, setuptools, easy_install 与 pip

nose

sudo apt intall python-nose

nose是用于python程序单元测试的第三方包

virtualenv

sudo apt intall python-virtualenv

virtualenv 用来创建隔离的Python环境

  • 建立一个虚拟环境:virtualenv 目录名

damao@damao:~$ virtualenv ~/Desktop/virtualpython

Running virtualenv with interpreter /usr/bin/python2
New python executable in /home/damao/Desktop/virtualpython/bin/python2
Also creating executable in /home/damao/Desktop/virtualpython/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.

  • 激活一个虚拟环境:进入上一步创建的目录执行 source bin/activate

damao@damao:~/Desktop/virtualpython$ source bin/activate

(virtualpython) damao@damao:~/Desktop/virtualpython$

激活成功的话,提示符前面会显示之前创建的虚拟环境的名称

  • 虚拟环境下安装包:使用pip或setup.py都行

## 在虚拟环境中安装bottle框架:

(virtualpython) damao@damao:~/Desktop/virtualpython$ pip install bottle

Collecting bottle
  Downloading bottle-0.12.9.tar.gz (69kB)
    100% |████████████████████████████████| 71kB 100kB/s
Building wheels for collected packages: bottle
  Running setup.py bdist_wheel for bottle ... done
  Stored in directory: /home/damao/.cache/pip/wheels/6e/87/89/f7ddd6721f4a208d44f2dac02f281b2403a314dd735d2b0e61
Successfully built bottle
Installing collected packages: bottle
Successfully installed bottle-0.12.9


## 进入虚拟环境的bin目录可以查看刚安装的bottle框架


(virtualpython) damao@damao:~/Desktop/virtualpython/bin$ ls

activate       activate_this.py  easy_install      pip2    python2        wheel
activate.csh   bottle.py         easy_install-2.7  pip2.7  python2.7
activate.fish  bottle.pyc        pip               python  python-config


  • 退出虚拟环境:deactivate

virtualenv文档

setup.py

try:
    from setuptools import setup
except ImportError:
    from distutils.core import setup


config = {
    'description':'My project',
    'author':'My name',
    'url':'URL to get it at.',
    'download_url':'Where to download it.',
    'author_email':'My email',
    'version':'0.1',
    'install_requires':['nose'],
    'packages':['NAME'],
    'scripts':[],
    'name':'projectname'
}

setup(**config)

setup.py是一个项目中必不可少的文件,使用

python setup.py sdist

可以分享你的包给社区,别人可以通过使用

python setup.py install

来安装你的包。

setup()是setuptools或者distutils.core中的一个函数,setup函数接收很多个参数,上例中的参数解释如下:

‘description’:关于该模块的单行描述
‘author’:模块作者
‘url’:模块主页
‘download_url’:模块下载链接
‘author_email’:作者电邮
‘version’:模块版本号
‘install_requires’:此模块依赖的模块,
‘packages’:告诉distutils需要处理的包
‘scripts’:指定源码文件,可从命令行执行
‘name’:模块名

distutils.core中setup函数的定义

setuptools文档

setuptools源码

writing the setup script

关于python中的setup.py

setup.py文件详解

以树形结构图显示目录

在Ubuntu系统中打开终端,输入命令tree

若之前没有安装过,会出现以下信息

owen@ubuntu:~/Documents/test/tests$ tree
The program ‘tree’ is currently not installed. You can install it by typing:
sudo apt-get install tree

根据提示,输入命令sudo apt-get install tree,开始tree程序安装

待安装完成后,在终端的任意目录下输入tree,即可以树形结构的方式显示该目录下的所有文件夹及文件

《《笨办法学Python》笔记33-----一个项目骨架》 tree命令

还可深入挖掘详细的tree命令用法,输入tree --help

《《笨办法学Python》笔记33-----一个项目骨架》 tree命令选项

    原文作者:大猫黄
    原文地址: https://www.jianshu.com/p/ec56c9f5cbca
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞