首頁  >  文章  >  後端開發  >  四個Python專案管理與建構工具,建議收藏!

四個Python專案管理與建構工具,建議收藏!

WBOY
WBOY轉載
2023-04-12 22:52:051385瀏覽

四個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 專案目錄結構

##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
cleanremove all build, test, coverage and Python artifacts
clean-buildremove build artifacts
clean-pycremove 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
servedocscompile the docs watching for changes
releasepackage and upload a release
dist builds source and wheel package
installinstall 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 -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 等

在我體驗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, 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 依次降低,使用的难度大略也是相同的顺序

以上是四個Python專案管理與建構工具,建議收藏!的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:51cto.com。如有侵權,請聯絡admin@php.cn刪除