1、CookieCutter
CookieCutter 一个经典的 Python 项目目录结构
[Python] 纯文本查看 复制代码 $ pip install cookiecutter
$ cookiecutter gh:audreyr/cookiecutter-pypackage
# 以 github 上的 audreyr/cookiecutter-pypackage 为模板,再回答一堆的问题生成一个 Python 项目
......
project_name [Python Boilerplate]: sample
......
最后由 cookiecutter 生成的项目模板是下面的样子:
[Python] 纯文本查看 复制代码 $ tree sample
sample
├── AUTHORS.rst
├── CONTRIBUTING.rst
├── HISTORY.rst
├── LICENSE
├── MANIFEST.in
├── Makefile
├── README.rst
├── docs
│ ├── Makefile
│ ├── authors.rst
│ ├── conf.py
│ ├── contributing.rst
│ ├── history.rst
│ ├── index.rst
│ ├── installation.rst
│ ├── make.bat
│ ├── readme.rst
│ └── usage.rst
├── requirements_dev.txt
├── sample
│ ├── __init__.py
│ ├── cli.py
│ └── sample.py
├── setup.cfg
├── setup.py
├── tests
│ ├── __init__.py
│ └── test_sample.py
└── tox.ini
3 directories, 26 files
这大概是当前比较流行的目录结构的主体框架,主要元素是:
[Python] 纯文本查看 复制代码 $ tree sample
sample
├── Makefile
├── README.rst
├── docs
│ └── index.rst
├── requirements.txt
├── sample
│ ├── __init__.py
│ └── sample.py
├── setup.cfg
├── setup.py
└── tests
├── __init__.py
└── test_sample.py
项目 sample 目录中重复 sample 目录中放置 Python 源文件,tests 目录中是测试文件,再加一个 docs 目录放文档,README.rst, 其他的用于构建的 setup, setup.cfg 和 Makefile 文件。
这其实是一个很经典的 Python 项目结构,接下来的构建就用 make 命令了,输入 make 会看到定义在 Makefile 文件中的指令
[Python] 纯文本查看 复制代码 $ make
clean remove all build, test, coverage and Python artifacts
clean-build remove build artifacts
clean-pyc remove Python file artifacts
clean-test remove test and coverage artifacts
lint check style
test run tests quickly with the default Python
test-all run tests on every Python version with tox
coverage check code coverage quickly with the default Python
docs generate Sphinx HTML documentation, including API docs
servedocs compile the docs watching for changes
release package and upload a release
dist builds source and wheel package
install install the package to the active Python's site-packages
为使用上面的构建过程,需要安装相应的包,如 tox, wheel, coverage, sphinx, flake8, 它们都可以通过 pip 来安装。之后就可以 make test, make coverage, make docs,make dist 等。其中 make docs 可以生成一个很漂亮的 Web 文档。
2、PyScaffold
PyScaffold 创建一个项目
PyScaffold 顾名思义,它是一个用来创建 Python 项目脚手架的工具,安装和使用:
[Python] 纯文本查看 复制代码 $ pip install pyscaffold
$ putup sample
这样创建了一个 Python 项目,目录结构与前面 cookiecutter 所选的模板差不多,只不过它把源文件放在了 src 目录,而非 sample 目录。
[Python] 纯文本查看 复制代码 $ tree sample
sample
├── AUTHORS.rst
├── CHANGELOG.rst
├── CONTRIBUTING.rst
├── LICENSE.txt
├── README.rst
├── docs
│ ├── Makefile
│ ├── _static
│ ├── authors.rst
│ ├── changelog.rst
│ ├── conf.py
│ ├── contributing.rst
│ ├── index.rst
│ ├── license.rst
│ ├── readme.rst
│ └── requirements.txt
├── pyproject.toml
├── setup.cfg
├── setup.py
├── src
│ └── sample
│ ├── __init__.py
│ └── skeleton.py
├── tests
│ ├── conftest.py
│ └── test_skeleton.py
└── tox.ini
整个项目的构建就要用 tox 这个工具了。tox 是一个自动化测试和构建工具,它在构建过程中可创建 Python 虚拟环境,这让测试和构建能有一个干净的环境。tox 使用教程
tox -av 能显示出定义在 tox.ini 中所有的任务:
[Python] 纯文本查看 复制代码 $ tox -av
default environments:
default -> Invoke pytest to run automated tests
additional environments:
build -> Build the package in isolation according to PEP517, see [url]https://github.com/pypa/build[/url]
clean -> Remove old distribution files and temporary build artifacts (./build and ./dist)
docs -> Invoke sphinx-build to build the docs
doctests -> Invoke sphinx-build to run doctests
linkcheck -> Check for broken links in the documentation
publish -> Publish the package you have been developing to a package index server. By default, it uses testpypi. If you really want to publish your package to be publicly accessible in PyPI, use the `-- --repository pypi` option.
【免责声明】本文部分系转载,原文来源 ython之禅,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如涉及作品内容、版权和其它问题,请在30日内与联系我们,我们会予以更改或删除相关文章,以保证您的权益!
|