Python 項目管理與構建工具:CookieCutter, PyScaffold, PyBuilder, Poetry

2021-12-31 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 用程序代碼來進行安裝與構建。關於項目目錄布局,有做成項目模板的,然後做成工具來應用項目模板。

下面大概瀏覽一下四個工具的使用

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.pypoetry 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/)

相關焦點

  • 四個 Python 項目管理與構建工具
    回到 Python,產生過 pip, pipenv, conda 那樣的包管理工具,但對項目的目錄布局沒有任何約定。關於構建很多還是延續了傳統的 Makefile 的方式,再就是加上 setup.py 和 build.py 用程序代碼來進行安裝與構建。關於項目目錄布局,有做成項目模板的,然後做成工具來應用項目模板。
  • 如何建立一個完美的 Python 項目?
    (代碼風格統一、類型檢測、測試覆蓋率高、自動檢測)在本文中,我將介紹如何建立一個可以做到這些點的項目。您可以按照步驟操作,也可以直接跳到 使用 cookiecutter 生成項目 部分(老手)。我們將使用它來安裝 pipenv 和 cookiecutter。
  • Poetry教程一(Poetry安裝與卸載)
    PoetryPoetry是一個依賴項管理和打包工具,它允許您聲明項目所依賴的庫,它將為您管理(安裝/更新)它們。
  • Python 炫技操作:安裝包的八種方法
    使用 pipxpipx 是一個專門用於安裝和管理 cli 應用程式的工具,使用它安裝的 Python 包會單獨安裝到一個全新的獨有虛擬環境。使用 setup.py如果你有編寫 setup.py 文件,可以使用如下命令直接安裝# 使用源碼直接安裝$ python setup.py install5.
  • Python 第三方庫大全看這一篇就夠了(1000+工具包)
    /vinta/awesome-python資源列表環境管理管理 Python 版本和環境的工具p:非常簡單的交互式 python 版本管理工具。pipenv:Python 官方推薦的新一代包管理工具。poetry:可完全取代 setup.py 的包管理工具。conda:跨平臺的 Python 二進位包管理工具。Curdling:管理 Python 包的命令行工具。wheel:Python 分發的新標準,意在取代 eggs。包倉庫本地 PyPI 倉庫服務和代理。
  • 如何使用Visual Studio工具創建python項目
    python一般情況下,我們使用Visual Studio進行C#、C++和C語言項目進行開發。隨著工具不斷完善,現在也可以進行python項目開發了。那麼,如何在Visual Studio 2019工具上創建python項目?操作步驟:1、通過快捷方式或電腦開始菜單,雙擊打開Visual Studio 2019工具
  • (收藏)哪些 Python 庫讓你相見恨晚?
    MarkdownYAMLCSVArchive環境管理管理 Python 版本和環境的工具p – 非常簡單的交互式 python 版本管理工具。pyenv – 簡單的 Python 版本管理工具。Vex – 可以在虛擬環境中執行命令。
  • 誰說Python不適合做GUI程序?wxFormBuilder上手教程
    集中營」,星標公眾號來源:https://blog.csdn.net/caiza3491/article/details/79529966前言很多人說Python不適合做桌面GUI程序開發,因為如果是桌面上的內容太多,全部手寫代碼確實不方便,還好有比如QT這樣的設計工具,今天介紹另外一個工具:wxFormBuilder
  • Python測試開發庫及項目(超全面!)
    https://github.com/pywinauto/pywinauto/SikuliX - 基於OpenCV的GUI測試框架,使用圖像識別來定位與之間的項目,來自python 2.7的腳本,跨平臺。
  • Python常用庫大全
    環境管理管理 Python 版本和環境的工具p – 非常簡單的交互式 python 版本管理工具。 pyenv – 簡單的 Python 版本管理工具。 pynsist – 一個用來創建 Windows 安裝程序的工具,可以在安裝程序中打包 Python本身。 構建工具將源碼編譯成軟體。buildout – 一個構建系統,從多個組件來創建,組裝和部署應用。
  • 數據科學家需要簡潔的Python代碼
    幸運的是,由於開源者的辛勤工作,有一種剛出爐的現成解決方案,用於創建我們想要的文件夾結構:cookiecutter。創建許多數據科學項目共有的簡潔文件夾結構只是一個命令。觀看下面的視頻,了解如何設置cookiecutter項目結構。
  • Python黑帽編程1.3 Python運行時與包管理工具
    解壓:root@kali:~# tar xzf python-nmap-0.6.0.tar.gzroot@kali:~# lsparos          python-nmapcopying nmap/__init__.py -> build/lib.linux-x86_64-2.7/nmapcopying nmap/test_nmap.py -> build/lib.linux-x86_64-2.7/nmapcopying nmap/nmap.py -> build/lib.linux-x86_64-2.7/nmapr
  • 100個相見恨晚的Python庫(建議收藏)
    >1環境管理管理 Python 版本和環境的工具p:非常簡單的交互式 Python 版本管理工具。pipenv:Python 官方推薦的新一代包管理工具。poetry:可完全取代 setup.py 的包管理工具。conda:跨平臺的 Python 二進位包管理工具。Curdling:管理 Python 包的命令行工具。wheel:Python 分發的新標準,意在取代 eggs。3包倉庫本地 PyPI 倉庫服務和代理。
  • 1000+ Python第三方庫大合集
    資源列表環境管理管理 Python 版本和環境的工具p:非常簡單的交互式 python 版本管理工具。virtualenv:創建獨立 Python 環境的工具。buildout:在隔離環境初始化後使用聲明性配置管理。包管理管理包和依賴的工具。pip-tools:保證 Python 包依賴關係更新的一組工具。PyPI:Python 正式的第三方包軟體存儲庫。pipenv:Python 官方推薦的新一代包管理工具。poetry:可完全取代 setup.py 的包管理工具。
  • Python 資源大全中文版
    版權是 https://github.com/jobbole/awesome-python-cn具體內容環境管理管理 Python 版本和環境的工具p:非常簡單的交互式 python 版本管理工具。官網pyenv:簡單的 Python 版本管理工具。官網Vex:可以在虛擬環境中執行命令。
  • python推薦[]超級工具anaconda
    沒有使用過anaconda鏡像的讀者對channel的配置不熟悉的讀者簡介anaconda是最好用python解釋器,裡面默認包含了大量的科學計算包,以及最厲害的包管理工具conda。推薦使用python3,python3 比python2 更快,bug更少。
  • 1000+ 常用 Python 庫一覽
    python-qrcode,純Python的二維碼(QR碼)生成器。pyBarcode,創建條碼,無需PIL模塊。pygram,Instagram像圖像過濾器。Quads,基於四叉樹的計算機藝術。nude.py,裸體檢測函數。scikit-image,scikit工具箱的圖像處理庫。
  • Python利用Django 構建Rest Api: 快速入門教程
    Python 3+Django 3 結合Vue.js框架構建前後端分離Web開發平臺實戰1.前言在本篇中,我們將為大家介紹如何基於Python構建一個以Django Rest框架為特徵的博客應用程式API服務,並且該API服務支持CRUD (Create-Read-Update-Delete)功能。PS: API(應用程式編程接口)為開發人員提供了與應用程式資料庫交互的接口。2. Django項目配置首先在本地為我們的代碼創建一個新的目錄。
  • Python常用第三方庫大全, 值得收藏!
    CGAL,Computational Geometry Algorithms Library,計算幾何算法庫,提供計算幾何相關的數據結構和算法,諸如三角剖分(2D約束三角剖分及二維和三維Delaunay三角剖分),Voronoi圖(二維和三維的點,2D加權Voronoi圖,分割Voronoi圖等),多邊形(布爾操作,偏置),多面體(布爾運算),曲線整理及其應用,網格生成(二維Delaunay
  • python程式設計師嘔心瀝血整理 Django 優秀資源大全
    cookiecutter, star:4912 - 一個可根據 cookiecutters (項目模板)創建新項目的命令行工具。django-hackathon-starter, star:996 - 一個 Django Web 應用的樣板,包含多種社交認證方法和多個流行的 API 示例(1 年未更新)。