成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

收藏!四個 Python 項目管理與構建工具

開發 后端
Python 歷時這么久以來至今還未有一個事實上標準的項目管理及構建工具,以至于造成 Python 項目的結構與構建方式五花八門。這或許是體現了 Python 的自由意志。

 [[434278]]

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 項目目錄結構 

  1. $ pip install cookiecutter  
  2. $ cookiecutter gh:audreyr/cookiecutter-pypackage    
  3. # 以 github 上的 audreyr/cookiecutter-pypackage 為模板,再回答一堆的問題生成一個 Python 項目 
  4. ......  
  5. project_name [Python Boilerplate]: sample  
  6. ...... 

最后由 cookiecutter 生成的項目模板是下面的樣子: 

  1. $ tree sample  
  2. sample  
  3. ├── AUTHORS.rst  
  4. ├── CONTRIBUTING.rst  
  5. ├── HISTORY.rst  
  6. ├── LICENSE  
  7. ├── MANIFEST.in  
  8. ├── Makefile  
  9. ├── README.rst  
  10. ├── docs  
  11. │   ├── Makefile  
  12. │   ├── authors.rst  
  13. │   ├── conf.py  
  14. │   ├── contributing.rst  
  15. │   ├── history.rst  
  16. │   ├── index.rst  
  17. │   ├── installation.rst  
  18. │   ├── make.bat  
  19. │   ├── readme.rst  
  20. │   └── usage.rst  
  21. ├── requirements_dev.txt  
  22. ├── sample  
  23. │   ├── __init__.py  
  24. │   ├── cli.py  
  25. │   └── sample.py  
  26. ├── setup.cfg  
  27. ├── setup.py  
  28. ├── tests  
  29. │   ├── __init__.py  
  30. │   └── test_sample.py  
  31. └── tox.ini  
  32. 3 directories, 26 files 

這大概是當前比較流行的目錄結構的主體框架,主要元素是: 

  1. $ tree sample  
  2. sample  
  3. ├── Makefile  
  4. ├── README.rst  
  5. ├── docs  
  6. │   └── index.rst  
  7. ├── requirements.txt  
  8. ├── sample  
  9. │   ├── __init__.py  
  10. │   └── sample.py  
  11. ├── setup.cfg  
  12. ├── setup.py  
  13. └── tests  
  14.     ├── __init__.py  
  15.     └── test_sample.py 

項目 sample 目錄中重復 sample 目錄中放置 Python 源文件,tests 目錄中是測試文件,再加一個 docs 目錄放文檔,README.rst, 其他的用于構建的 setup, setup.cfg 和 Makefile 文件。

這其實是一個很經典的 Python 項目結構,接下來的構建就用 make 命令了,輸入 make 會看到定義在 Makefile 文件中的指令。

  1. $ make  
  2. clean                remove all build, test, coverage and Python artifacts  
  3. clean-build          remove build artifacts  
  4. clean-pyc            remove Python file artifacts  
  5. clean-test           remove test and coverage artifacts  
  6. lint                 check style  
  7. test                 run tests quickly with the default Python  
  8. test-all             run tests on every Python version with tox  
  9. coverage             check code coverage quickly with the default Python  
  10. docs                 generate Sphinx HTML documentation, including API docs  
  11. servedocs            compile the docs watching for changes  
  12. release              package and upload a release  
  13. dist                 builds source and wheel package  
  14. 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 項目腳手架的工具,安裝和使用: 

  1. $ pip install pyscaffold  
  2. $ putup sample 

這樣創建了一個 Python 項目,目錄結構與前面  cookiecutter 所選的模板差不多,只不過它把源文件放在了 src 目錄,而非 sample 目錄。 

  1. $ tree sample  
  2. sample  
  3. ├── AUTHORS.rst  
  4. ├── CHANGELOG.rst  
  5. ├── CONTRIBUTING.rst  
  6. ├── LICENSE.txt  
  7. ├── README.rst  
  8. ├── docs  
  9. │   ├── Makefile  
  10. │   ├── _static  
  11. │   ├── authors.rst  
  12. │   ├── changelog.rst  
  13. │   ├── conf.py  
  14. │   ├── contributing.rst  
  15. │   ├── index.rst  
  16. │   ├── license.rst  
  17. │   ├── readme.rst  
  18. │   └── requirements.txt  
  19. ├── pyproject.toml  
  20. ├── setup.cfg  
  21. ├── setup.py  
  22. ├── src  
  23. │   └── sample  
  24. │       ├── __init__.py  
  25. │       └── skeleton.py  
  26. ├── tests  
  27. │   ├── conftest.py  
  28. │   └── test_skeleton.py  
  29. └── tox.ini 

整個項目的構建就要用 tox 這個工具了。tox 是一個自動化測試和構建工具,它在構建過程中可創建 Python 虛擬環境,這讓測試和構建能有一個干凈的環境。

  1. tox -av 能顯示出定義在 tox.ini 中所有的任務:  
  2. $ tox -av  
  3. default environments:  
  4. default   -> Invoke pytest to run automated tests  
  5. additional environments:  
  6. build     -> Build the package in isolation according to PEP517, see https://github.com/pypa/build  
  7. clean     -> Remove old distribution files and temporary build artifacts (./build and ./dist)  
  8. docs      -> Invoke sphinx-build to build the docs  
  9. doctests  -> Invoke sphinx-build to run doctests  
  10. linkcheck -> Check for broken links in the documentation  
  11. 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 命令過程中,每一步好像都比較慢,應該是創建虛擬機要花些時間。

 PyBuilder

最好再看另一個構建工具 PyBuilder, 它所創建出的目錄結構很接近于 Maven, 下面來瞧瞧。

  1. $ pip install pybuilder  
  2. $ mkdir sample && cd sample    # 項目目錄需手工創建  
  3. $ pyb --start-project          # 回答一些問題后創建所需的目錄和文件 

完后看下它的目錄結構: 

  1. $ tree sample  
  2.  
  3. ├── build.py  
  4. ├── docs  
  5. ├── pyproject.toml  
  6. ├── setup.py  
  7. └── src  
  8.     ├── main  
  9.     │   ├── python  
  10.     │   └── scripts  
  11.     └── unittest  
  12.         └── python 

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

  1. $ pyb -t sample  
  2. Tasks found for project "sample":  
  3.                   analyze -  Execute analysis plugins.  
  4.                             depends on tasks: prepare run_unit_tests  
  5.                     clean - Cleans the generated output.  
  6.           compile_sources - Compiles source files that need compilation.  
  7.                             depends on tasks: prepare  
  8.                  coverage - <no description available>  
  9.                             depends on tasks: verify  
  10.                   install - Installs the published project.  
  11.                             depends on tasks: package publish(optional)  
  12.                   package - Packages the application. Package a python application.  
  13.                             depends on tasks: compile_sources run_unit_tests(optional)  
  14.                   prepare - Prepares the project for building. Creates target VEnvs  
  15.         print_module_path - Print the module path.  
  16.        print_scripts_path - Print the script path.  
  17.                   publish - Publishes the project.  
  18.                             depends on tasks: package verify(optional) coverage(optional)  
  19.     run_integration_tests - Runs integration tests on the packaged application.  
  20.                             depends on tasks: package  
  21.            run_unit_tests - Runs all unit tests. Runs unit tests based on Python's unittest module  
  22.                             depends on tasks: compile_sources  
  23.                    upload - Upload a project to PyPi.  
  24.                    verify - Verifies the project and possibly integration tests.  
  25.                             depends on tasks: run_integration_tests(optional)  
  26. $ pyb run_unit_tests sample 

PyBuilder 也是在構建或測試之前創建虛擬環境, 從 0.12.9 版開始可通過參數 --no-venvs 跳過創建虛擬環境這一步。使用了 --no-venvs 的話 Python 代碼將會在運行  pyb 的當前 Python 環境中執行,所需的依賴將要手工安裝。

項目的依賴也要定義在 build.py 文件中。

  1. @init  
  2. def set_properties(project):  
  3.     project.depends_on('boto3', '>=1.18.52')  
  4.     project.build_depends_on('mock') 

隨后在執行 pyb 創建虛擬環境時就會安裝上面的依賴,并在其中運行測試與構建。

 Poetry

最后一個 Poetry, 感覺這是一個更為成熟,項目活躍度也更高的 Python 構建,它有著更強大的信賴管理功能,用 poetry add boto3 就能添加依賴,poetry show --tree 顯示出依賴樹。看下如何安裝及創建一個項目。 

  1. $ pip install poetry  
  2. $ poetry new sample

它創建的項目比上面都簡單:

  1. $ tree sample  
  2. sample  
  3. ├── README.rst  
  4. ├── pyproject.toml  
  5. ├── sample  
  6. │   └── __init__.py  
  7. └── tests  
  8.     ├── __init__.py  
  9.     └── 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 命令的行。

  1. # 往 pyproject.toml 中添加對  boto3 的依賴并安裝(add 還能從本地或 git 來安裝依賴 ),  
  2. poetry add boto3      
  3.  # 將依照 pyproject.toml 文件中定義安裝相應的依賴到當前的 Python 虛擬環境中  
  4.  # 比如在 <test-venv>/lib/python3.9/site-packages 目錄中,安裝好模塊后也可讓測試用例使用  
  5. poetry install        

其他主要的:

  1. 1.  poetry build    # 構建可安裝的 *.whl 和 tar.gz 文件  
  2. 2.  poetry shell    # 會根據定義在 pyproject.toml 文件中的依賴創建并使用虛擬環境  
  3. 3.  poetry run pytest    # 運行使用 pytest 的測試用例,如 tests/test_sample.py  
  4. 4.  poetry run python -m unittest tests/sample_tests.py  # 運行 unittest 測試用例  
  5. 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, 內容為:

  1. def main():  
  2.     print('hello poetry') 

然后在 pyproject.toml 中寫上 

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

再執行 

  1. $ poetry run my-script 

就會輸出 "hello poetry"。

通過對以上四個工具的認識,項目結構的復雜度由 cookiecutter-pyproject -> PyScaffold -> PyBuilder -> Poetry 依次降低,使用的難度大略也是相同的順序。 

 

責任編輯:龐桂玉 來源: Python之禪
相關推薦

2022-08-12 07:56:41

Python項目管理構建工具

2023-09-13 11:05:22

物聯網平臺物聯網

2020-08-13 10:29:55

項目管理項目經理CIO

2023-11-20 22:35:32

2013-01-31 10:15:28

JavaScriptGrunt

2011-12-30 09:23:25

JavaPhing

2021-05-25 16:34:06

JavaScript前端

2022-06-02 08:00:00

數據科學機器學習工具

2018-04-18 21:55:59

多云架構云計算數據

2023-08-22 10:13:53

模塊工具JavaScrip

2023-03-15 23:59:13

前端構建工具

2024-06-04 22:04:39

2022-03-25 14:11:11

Java死鎖線程

2025-03-26 01:00:00

2010-02-03 15:09:13

Python 構建工具

2022-07-15 14:54:00

DockerLinux技巧

2024-10-25 15:43:37

2020-09-07 14:40:20

Vue.js構建工具前端

2022-02-23 15:09:18

數字化轉型國有企業數據

2021-06-18 06:11:26

工具WebpackSnowpack
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 一级一级毛片免费看 | 国产情品| 国外成人在线视频 | 日韩久久久久久久 | 亚洲电影一区二区三区 | 日韩国产在线 | 成年人在线视频 | 成人黄色三级毛片 | 日本色综合 | 精品视频一区二区在线观看 | 精品国产伦一区二区三区观看体验 | 久久久www成人免费精品张筱雨 | 二区亚洲 | 亚洲最大av网站 | 99re视频这里只有精品 | 欧美精品1区 | 91九色porny首页最多播放 | 91热在线 | 亚洲精品自在在线观看 | 精品免费在线 | 性天堂网 | 欧美日韩国产一区二区 | 久久男人 | av中文在线| 999久久久免费精品国产 | 911影院| 91在线精品播放 | 成人性生交大免费 | 91视频进入 | 精品www| 亚洲第一av | 免费一区二区三区 | 91视频一88av | www.成人.com | 蜜桃av人人夜夜澡人人爽 | 91亚洲免费 | 2022精品国偷自产免费观看 | 4h影视 | 久久免费精品视频 | 中文久久| 久久久久久久97 |