4个Python项目管理与构建工具,建议收藏!(4个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 samplesample├── 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.ini3 directories, 26 files

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

$ tree samplesample├── 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 文件中的指令

$ makeclean remove all build, test, coverage and Python artifactsclean-build remove build artifactsclean-pyc remove Python file artifactsclean-test remove test and coverage artifactslint check styletest run tests quickly with the default Pythontest-all run tests on every Python version with toxcoverage check code coverage quickly with the default Pythondocs generate Sphinx HTML documentation, including API docsservedocs compile the docs watching for changesrelease package and upload a releasedist builds source and wheel packageinstall 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 samplesample├── 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 -av 能显示出定义在 tox.ini 中所有的任务:

$ tox -avdefault environments:default -> Invoke pytest to run automated testsadditional environments:build -> Build the package in isolation according to PEP517, see https://github.com/pypa/buildclean -> Remove old distribution files and temporary build artifacts (./build and ./dist)docs -> Invoke sphinx-build to build the docsdoctests -> Invoke sphinx-build to run doctestslinkcheck -> Check for broken links in the documentationpublish -> 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

在我体验 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 sampleTasks 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 文件中

@initdef 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 samplesample├── 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 有些类似 NodeJSpackage.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.py4. 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 依次降低,使用的难度大略也是相同的顺序

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

(0)
上一篇 2023年7月28日 上午9:25
下一篇 2023年7月28日 上午9:35

相关推荐

  • 高校办公协同系统

    高校办公协同系统: 促进团队协作与效率提升 随着信息技术的不断发展,高校办公协同系统已成为现代办公的重要组成部分。高校办公协同系统旨在提高学校内部的工作效率和团队协作能力,促进教学…

    科研百科 2024年9月4日
    128
  • 经验交流:构建党建工作新机制,提升党建管理新水平(加强党建工作机制创新)

    文/孙秀丽 基层党支部开展主题党日活动 党建检查考核工作是推动党的理论和路线方针政策、上级党组织决策部署贯彻落实的重要手段,是改进党的作风、 激励广大干部担当作为的重要举措。 20…

    科研百科 2023年10月20日
    129
  • 好选客SCRM软件-盘活积累的外贸客户(好选是什么意思)

    SCRM系统是什么? 社交客户关系管理(SCRM)系统是一个以客户数据的管理为核心,为企业收集客户信息,记录营销过程中与客户发生的各种交互行为并加以分析利用的信息系统,大多具备客户…

    科研百科 2023年4月13日
    308
  • 马军信息系统项目管理

    马军是一位信息系统项目管理专家,他在过去的几十年中一直致力于帮助公司和组织实现信息和业务流程的自动化。在他的职业生涯中,马军领导了许多成功的信息系统项目,这些项目对于公司的业务增长…

    科研百科 2024年9月24日
    25
  • 火炬开发区:坚持党建引领,探索社区基层社会治理创新

    【编者按】 为推动中山火炬开发区掀起深入学习贯彻落实党的二十大精神的热潮,充分展现各部门、单位的学习新风采、工作新思路,南方 火炬区频道即日起推出系列报道,敬请垂注! 党的二十大报…

    科研百科 2023年9月28日
    176
  • 科研资料网站(科研资料管理系统)

    科研资料管理系统 科研资料管理系统是一种用于管理和维护科研资料的软件系统,它可以帮助研究人员更好地组织、存储、管理和共享他们的研究资料。科研资料管理系统对于研究人员来说非常重要,因…

    科研百科 2024年8月9日
    56
  • Linux搭建开源企业邮箱系统EwoMail(基于linux系统的开源企业邮箱构建)

    EwoMail是什么 EwoMail是基于Linux的开源邮件服务器软件,集成了众多优秀稳定的组件,是一个快速部署、简单高效、多语言、安全稳定的邮件解决方案,帮助你提升运维效率,降…

    2022年6月22日
    671
  • 管理信息系统项目计划书

    管理信息系统项目计划书 随着信息技术的不断发展,企业对于管理信息系统的需求也在不断增加。管理信息系统是一种能够帮助企业进行有效管理的工具,它可以帮助企业提高效率,降低成本,提升服务…

    科研百科 2025年1月28日
    0
  • 科研项目招标什么意思呀

    科研项目招标是指一种项目管理方式,通过公开招标的方式选择最适合该项目的供应商或承包商。这种方式通常是由政府部门或学术机构组织的,旨在寻找最优秀的供应商或承包商来为该项目提供技术支持…

    科研百科 2025年5月16日
    1
  • 河南:学生社团管理“有法可依”

    中国教育报-中国教育新闻网讯(记者 李见新)近日,河南省教育厅印发《河南省中小学校学生社团暂行管理办法》。办法提出,学校要为学生社团提供师资、场地、器材设备、活动经费等方面的支持,…

    科研百科 2023年10月7日
    158