Python 项管理与构建工具:CookieCutter, PyScaffold, PyBuilder
qiyuwang 2024-10-31 15:51 30 浏览 0 评论
作者:隔叶黄莺
来源:https://yanbin.blog/python-dependency-management-build-tools
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 用程序代码来进行安装与构建。关于项目目录布局,有做成项目模板的,然后做成工具来应用项目模板。
下面大概浏览一下四个工具的使用
- CookieCutter
- PyScaffold
- PyBuilder
- 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 docs,make 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 命令的行
- poetry add boto3 # 往 pyproject.toml 中添加对 boto3 的依赖并安装(add 还能从本地或 git 来安装依赖 ), 用 --dev 参数是给开发时用的
- poetry install # 将依照 pyproject.toml 文件中定义安装相应的依赖到当前的 Python 虚拟环境中,比如在 <test-venv>/lib/python3.9/site-packages 目录中,安装好模块后也可让测试用例使用
其他主要的
- poetry build # 构建可安装的 *.whl 和 tar.gz 文件
- poetry shell # 会根据定义在 pyproject.toml 文件中的依赖创建并使用虚拟环境
- poetry run pytest # 运行使用 pytest 的测试用例,如 tests/test_sample.py
- poetry run python -m unittest tests/sample_tests.py # 运行 unittest 测试用例
- poetry export --without-hashes --output requirements.txt # 导出 requirements.txt 文件, --dev 导出含 dev 的依赖,或者用 poetry export --without-hashes > requirements.txt
poetry run 能执行任何系统命令,只是它会在它要的虚拟环境中执行。所以可以想见,poetry 的项目要生成文档或覆盖率都必须用 poetry run ... 命令来支持 sphinx, coverage 或 flake8。
在 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 依次降低,使用的难度大略也是相同的顺序。
参考链接
- 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)
- Dependency management tools for Python (https://snyk.io/blog/dependency-management-tools-python/)
相关推荐
- 基于Docker方式安装与部署Camunda流程引擎
-
1Camunda简介官网:https://docs.camunda.org/manual/7.19/installation/docker/Camunda是一个轻量级、开源且高度灵活的工作流和决策自...
- 宝塔Linux面板如何部署Java项目?(宝塔面板 linux)
-
通过宝塔面板部署Java还是很方便的,至少不需要自己输入tomcat之类的安装命令了。在部署java项目前,我还是先说下目前的系统环境,如果和我的系统环境不一样,导致部署不成功,那你可能需要去找其他资...
- 浪潮服务器如何用IPMI安装Linux系统
-
【注意事项】此处以浪潮服务器为例进行演示所需使用的软件:Chrome浏览器个人PC中需要预先安装java,推荐使用jdk-8u181-windows-x64.exe【操作步骤】1、在服务器的BIOS中...
- Centos7环境Hadoop3集群搭建(hadoop集群环境搭建实验报告)
-
由于项目需要存储历史业务数据,经过评估数据量会达到100亿以上,在原有mongodb集群和ES集群基础上,需要搭建Hbase集群进行调研,所以首先总结一下Hadoop集群的搭建过程。一、三个节点的集群...
- Hadoop高可用集群搭建及API调用(hadoop高可用原理)
-
NameNodeHA背景在Hadoop1中NameNode存在一个单点故障问题,如果NameNode所在的机器发生故障,整个集群就将不可用(Hadoop1中虽然有个SecorndaryNameNo...
- 使用Wordpress搭建一个属于自己的网站
-
现在开源的博客很多,但是考虑到wordpress对网站的seo做的很好,插件也多。并且全世界流量排名前1000万的网站有33.4%是用Wordpress搭建的!所以尝试用Wordpress搭建一个网站...
- Centos 安装 Jenkins(centos 安装ssh)
-
1、Java安装查看系统是否已安装Javayumlistinstalled|grepjava...
- Java教程:gitlab-使用入门(java中的git)
-
1导读本教程主要讲解了GitLab在项目的环境搭建和基本的使用,可以帮助大家在企业中能够自主搭建GitLab服务,并且可以GitLab中的组、权限、项目自主操作...
- Dockerfile部署Java项目(docker部署java应用)
-
1、概述本文主要会简单介绍什么是Docker,什么是Dockerfile,如何安装Docker,Dockerfile如何编写,如何通过Dockerfile安装jar包并外置yaml文件以及如何通过do...
- 如何在Eclipse中搭建Zabbix源码的调试和开发环境
-
Zabbix是一款非常优秀的企业级软件,被设计用于对数万台服务器、虚拟机和网络设备的数百万个监控项进行实时监控。Zabbix是开放源码和免费的,这就意味着当出现bug时,我们可以很方便地通过调试源码来...
- Java路径-02-Java环境配置(java环境搭建及配置教程)
-
1Window环境配置1.1下载...
- 35.Centos中安装python和web.py框架
-
文章目录前言1.Centos7python:2.Centos8python:3.进行下载web.py框架然后应用:4.安装好之后进行验证:5.总结:前言...
- 《我的世界》服务器搭建(我的世界服务器如何搭建)
-
1.CentOS7环境1.1更改YUM源#下载YUM源文件curl-o/etc/yum.repos.d/CentOS-Base.repohttps://mirrors.aliyun.com...
- CentOS 7 升级 GCC 版本(centos7.4升级7.5)
-
1.GCC工具介绍GCC编译器:...
- Linux安装Nginx详细教程(linux安装配置nginx)
-
环境准备1.因为Nginx依赖于gcc的编译环境,所以,需要安装编译环境来使Nginx能够编译起来。命令:yuminstallgcc-c++显示完毕,表示安装完成:2.Nginx的http模块需要...
你 发表评论:
欢迎- 一周热门
- 最近发表
-
- 基于Docker方式安装与部署Camunda流程引擎
- 宝塔Linux面板如何部署Java项目?(宝塔面板 linux)
- 浪潮服务器如何用IPMI安装Linux系统
- Centos7环境Hadoop3集群搭建(hadoop集群环境搭建实验报告)
- Hadoop高可用集群搭建及API调用(hadoop高可用原理)
- 使用Wordpress搭建一个属于自己的网站
- Centos 安装 Jenkins(centos 安装ssh)
- Java教程:gitlab-使用入门(java中的git)
- Dockerfile部署Java项目(docker部署java应用)
- 如何在Eclipse中搭建Zabbix源码的调试和开发环境
- 标签列表
-
- navicat无法连接mysql服务器 (65)
- 下横线怎么打 (71)
- flash插件怎么安装 (60)
- lol体验服怎么进 (66)
- ae插件怎么安装 (62)
- yum卸载 (75)
- .key文件 (63)
- cad一打开就致命错误是怎么回事 (61)
- rpm文件怎么安装 (66)
- linux取消挂载 (81)
- ie代理配置错误 (61)
- ajax error (67)
- centos7 重启网络 (67)
- centos6下载 (58)
- mysql 外网访问权限 (69)
- centos查看内核版本 (61)
- ps错误16 (66)
- nodejs读取json文件 (64)
- centos7 1810 (59)
- 加载com加载项时运行错误 (67)
- php打乱数组顺序 (68)
- cad安装失败怎么解决 (58)
- 因文件头错误而不能打开怎么解决 (68)
- js判断字符串为空 (62)
- centos查看端口 (64)