收藏!四个 Python 项目管理与构建工具(Python项目管理)

Python 历时这么久以来至今还未有一个事实上标准的项目管理及构建工具,以至于造成 Python 项目的结构与构建方式五花八门。这或许是体现了 Python 的自由意志。

不像 Java 在经历了最初的手工构建,到半自动化的 Ant, 再到 Maven 基本就是事实上的标准了。其间 Maven 还接受了其他的 Gradle(Android 项目主推), SBT(主要是 Scala 项目), Ant Ivy, Buildr 等的挑战,但都很难撼动 Maven 的江湖地位,而且其他的差不多遵循了 Maven 的目录布局。

回到 Python,产生过 pip, pipenv, conda 那样的包管理工具,但对项目的目录布局没有任何约定。

关于构建很多还是延续了传统的 Makefile 的方式,再就是加上 setup.py 和 build.py 用程序代码来进行安装与构建。关于项目目录布局,有做成项目模板的,然后做成工具来应用项目模板。

下面大概浏览一下四个工具的使用

  1. CookieCutter

  2. PyScaffold

  3. PyBuilder

  4. Poetry

CookieCutter 一个经典的 Python 项目目录结构

$ pip install cookiecutter
$ cookiecutter gh:audreyr/cookiecutter-pypackage
# 以 github 上的 audreyr/cookiecutter-pypackage 为模板,再回答一堆的问题生成一个 Python 项目
......
project_name [Python Boilerplate]: sample
......

最后由 cookiecutter 生成的项目模板是下面的样子:

$ 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

这大概是当前比较流行的目录结构的主体框架,主要元素是:

$ 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 文件中的指令

$ 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 docsmake dist等。其中make docs可以生成一个很漂亮的 Web 文档。

PyScaffold 创建一个项目

PyScaffold 顾名思义,它是一个用来创建 Python 项目脚手架的工具,安装和使用:

$ pip install pyscaffold
$ putup sample

这样创建了一个 Python 项目,目录结构与前面 cookiecutter 所选的模板差不多,只不过它把源文件放在了 src目录,而非sample目录。

$ 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中所有的任务:

$ tox -av
default environments:
default -> Invoke pytest to run automated tests

additional environments:
build -> Build the package in isolation according to PEP517, see https://github.com/pypa/build
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.

要执行哪个命令便用 tox -e build,tox -e docs等, 下面是如何使用 PyScaffold 的动图:https://yanbin.blog/wp-content/uploads/2021/09/pyscaffold-demo.gif

在我体验 tox 命令过程中,每一步好像都比较慢,应该是创建虚拟机要花些时间。tox 使用教程

PyBuilder

最好再看另一个构建工具 PyBuilder, 它所创建出的目录结构很接近于 Maven, 下面来瞧瞧

$pip install pybuilder
$mkdir sample && cd sample # 项目目录需手工创建
$pyb --start-project # 回答一些问题后创建所需的目录和文件

完后看下它的目录结构:

$ tree sample
.
├── build.py
├── docs
├── pyproject.toml
├── setup.py
└── src
├── main
│ ├── python
│ └── scripts
└── unittest
└── python

构建过程仍然是用 pyb命令,可用pyb -h查看帮助,pyb -t列出所有的任务, PyBuilder 的任务是以插件的方式加入的,插件配置在build.py文件中。

$ pyb -t sample
Tasks found for project \"sample\":
analyze - Execute analysis plugins.
depends on tasks: prepare run_unit_tests
clean - Cleans the generated output.
compile_sources - Compiles source files that need compilation.
depends on tasks: prepare
coverage - <no description available>
depends on tasks: verify
install - Installs the published project.
depends on tasks: package publish(optional)
package - Packages the application. Package a python application.
depends on tasks: compile_sources run_unit_tests(optional)
prepare - Prepares the project for building. Creates target VEnvs
print_module_path - Print the module path.
print_scripts_path - Print the script path.
publish - Publishes the project.
depends on tasks: package verify(optional) coverage(optional)
run_integration_tests - Runs integration tests on the packaged application.
depends on tasks: package
run_unit_tests - Runs all unit tests. Runs unit tests based on Python\'s unittest module
depends on tasks: compile_sources
upload - Upload a project to PyPi.
verify - Verifies the project and possibly integration tests.
depends on tasks: run_integration_tests(optional)
$ pyb run_unit_tests sample

PyBuilder 也是在构建或测试之前创建虚拟环境, 从 0.12.9 版开始可通过参数 --no-venvs跳过创建虚拟环境这一步。使用了--no-venvs的话 Python 代码将会在运行pyb的当前 Python 环境中执行,所需的依赖将要手工安装。

项目的依赖也要定义在 build.py文件中

@init
def set_properties(project):
project.depends_on(\'boto3\', \'>=1.18.52\')
project.build_depends_on(\'mock\')

随后在执行 pyb创建虚拟环境时就会安装上面的依赖,并在其中运行测试与构建。

Poetry

最后一个 Poetry, 感觉这是一个更为成熟,项目活跃度也更高的 Python 构建,它有着更强大的信赖管理功能,用 poetry add boto3就能添加依赖,poetry show --tree显示出依赖树。看下如何安装及创建一个项目

$ pip install poetry
$ poetry new sample

它创建的项目比上面都简单

$ tree sample
sample
├── README.rst
├── pyproject.toml
├── sample
│ └── __init__.py
└── tests
├── __init__.py
└── test_sample.py

如果给 poetry new带上--src参数,那么源文件目录sample会放在src目录下,即sample/src/sample.

poetry init会在当前目录中生成pyproject.toml文件,目录等的生成需手动完成。

它不关注文档的生成,代码规范的检查,代码覆盖率都没有。它的项目配置更集中,全部在 pyproject.toml文件中,toml是什么呢?它是一种配置文件的格式 Tom\’s Obvious, Minimal Language (https://github.com/toml-lang/toml).

pyproject.toml有些类似 NodeJS 的package.json文件,比如 poetry add, poetry install 命令的行

# 往 pyproject.toml 中添加对 boto3 的依赖并安装(add 还能从本地或 git 来安装依赖 ),
poetry add boto3

# 将依照 pyproject.toml 文件中定义安装相应的依赖到当前的 Python 虚拟环境中
# 比如在 <test-venv>/lib/python3.9/site-packages 目录中,安装好模块后也可让测试用例使用
poetry install

其他主要的

1. poetry build # 构建可安装的 *.whl 和 tar.gz 文件
2. poetry shell # 会根据定义在 pyproject.toml 文件中的依赖创建并使用虚拟环境
3. poetry run pytest # 运行使用 pytest 的测试用例,如 tests/test_sample.py
4. poetry run python -m unittest tests/sample_tests.py # 运行 unittest 测试用例
5. poetry export --without-hashes --output requirements.txt # 导出 requirements.txt 文件, --dev 导出含 dev 的依赖,或者用 poetry export --without-hashes > requirements.txt

poetry run能执行任何系统命令,只是它会在它要的虚拟环境中执行。所以可以想见,poetry的项目要生成文档或覆盖率都必须用poetry run ...命令来支持sphinx,coverageflake8

在 sample 目录(与 pyproject.toml 文件平级)中创建文件 my_module.py, 内容为

def main:
print(\'hello poetry\')

然后在 pyproject.toml中写上

[tool.poetry.scripts]
my-script=\"sample.my_module:main\"

再执行

$ poetry run my-script

就会输出 \”hello poetry\”。

通过对以上四个工具的认识,项目结构的复杂度由 cookiecutter-pyproject -> PyScaffold -> PyBuilder -> Poetry 依次降低,使用的难度大略也是相同的顺序。

参考链接

  1. Set up tests, linters and type checking in Python projects in 2020 (https://medium.com/@cristobalcl/set-up-tests-linters-and-type-checking-in-python-projects-in-2020-9cc1b1e2750d) (介绍了 poetry 项目如何支持 coverage, lint 和 type checking)

  2. Dependency management tools for Python (https://snyk.io/blog/dependency-management-tools-python/)

来源:隔叶黄莺

链接:https://yanbin.blog/python-dependency-management-build-tools

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

(0)
上一篇 2022年8月23日 上午9:56
下一篇 2022年8月23日 上午9:58

相关推荐

  • 什么是SRM系统?有什么作用?如何应用SRM系统?

    在目前全球经济增长乏力、竞争加剧的经济背景下,企业要在全球市场竞争中胜出,很大程度上取决于供应链管理的能力(供应链是指生产及流通过程中,涉及将产品或服务提供给最终用户活动的上游与下…

    科研百科 2022年11月24日
    203
  • 导师发科研项目怎么回复

    尊敬的导师, 非常感谢您对我们科研项目的关注和支持。我们已经认真调研了您提供的研究方向,并进行了初步的研究工作。 根据我们的初步研究成果,我们发现该研究方向具有较大的潜力和可行性。…

    科研百科 2025年4月6日
    1
  • 大型投资周报:数字借贷平台Stashfin获2亿美元债权融资(数字借款)

    大型投资周报是「嘉程商业评论」整理的国内外大型TMT并购、融资事件,旨在及时提供最新投资信息、行情参考。 国外融资 本周共有59起2000万美元以上的海外融资事件,其中:活跃的行业…

    科研百科 2024年2月16日
    171
  • 科研项目软件核算

    科研项目软件核算:助力科研管理 随着科技的快速发展,科研项目管理软件已经成为了科研管理中不可或缺的一部分。这些软件能够帮助研究人员更好地管理科研项目,提高科研效率,减少人工错误,并…

    科研百科 2025年2月22日
    0
  • 公司合同管理总结

    公司合同管理总结 合同是公司日常运营中不可或缺的一部分,合同管理则是确保合同有效性和合规性的重要环节。在过去的几年中,我们公司一直在致力于加强合同管理,以确保公司的业务运营合法合规…

    科研百科 2024年8月23日
    30
  • Android版微信内测推送 支持计步功能(android版微信内测推送 支持计步功能吗)

    | 责编:刘菲菲 【中关村在线软件资讯】5月8日消息:微信着实有好一段时间没有更新了,不过腾讯可能意思是要更新就来一次大的,所以就有了这次即将到来的新版。 日前已经有部分Andro…

    科研百科 2024年4月29日
    71
  • 云南工程项目管理系统

    云南工程项目管理系统 云南工程项目管理系统是一种用于管理云南工程项目的软件系统,它可以帮助项目经理和管理人员更好地协调和沟通项目工作,提高项目的效率和质量。 云南工程项目管理系统采…

    科研百科 2025年1月6日
    1
  • 建筑工程材料管理中普遍存在的问题,如何解决?(工程项目材料管理存在的问题)

    建筑工程项目中,工程材料管理作为一个重要的部分,作为基础部分之一,管理的好坏将会对整个建筑的质量安全、结构功能以及项目的盈亏产生重要的影响。建筑工程材料管理是实现工程程序化、规范化…

    2022年6月13日
    218
  • 科研项目编号 科研项目编号在哪里查询

    科研项目编号查询 科研项目编号是用于标识和跟踪科研项目的重要标识符。这些编号通常由字母和数字组成,用于区分不同的科研项目。在项目完成后,通常会生成一个唯一的科研项目编号,以便跟踪和…

    科研百科 2024年7月30日
    78
  • 科研项目答辩授权函范文

    科研项目答辩授权函范文 尊敬的评审委员会: 我代表XXX研究小组,向贵委员会提交本次科研项目答辩授权函,授权代表我小组进行科研项目答辩。 本次科研项目XXX,旨在XXX,研究内容涉…

    科研百科 2025年4月16日
    1