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

收藏!四個 Python 項目管理與構(gòu)建工具

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

 [[434278]]

Python 歷時這么久以來至今還未有一個事實上標準的項目管理及構(gòu)建工具,以至于造成 Python 項目的結(jié)構(gòu)與構(gòu)建方式五花八門。這或許是體現(xiàn)了 Python 的自由意志。

不像 Java 在經(jīng)歷了最初的手工構(gòu)建,到半自動化的 Ant, 再到 Maven 基本就是事實上的標準了。其間 Maven 還接受了其他的 Gradle(Android 項目主推), SBT(主要是 Scala 項目), Ant+Ivy, Buildr 等的挑戰(zhàn),但都很難撼動 Maven 的江湖地位,而且其他的差不多遵循了 Maven 的目錄布局。

回到 Python,產(chǎn)生過 pip, pipenv, conda 那樣的包管理工具,但對項目的目錄布局沒有任何約定。

關(guān)于構(gòu)建很多還是延續(xù)了傳統(tǒng)的 Makefile 的方式,再就是加上 setup.py 和 build.py 用程序代碼來進行安裝與構(gòu)建。關(guān)于項目目錄布局,有做成項目模板的,然后做成工具來應(yīng)用項目模板。

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

  1.  CookieCutter
  2.  PyScaffold
  3.  PyBuilder
  4.  Poetry

 CookieCutter 一個經(jīng)典的 Python 項目目錄結(jié)構(gòu) 

  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 

這大概是當前比較流行的目錄結(jié)構(gòu)的主體框架,主要元素是: 

  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 目錄中重復(fù) sample 目錄中放置 Python 源文件,tests 目錄中是測試文件,再加一個 docs 目錄放文檔,README.rst, 其他的用于構(gòu)建的 setup, setup.cfg 和 Makefile 文件。

這其實是一個很經(jīng)典的 Python 項目結(jié)構(gòu),接下來的構(gòu)建就用 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 

為使用上面的構(gòu)建過程,需要安裝相應(yīng)的包,如 tox, wheel, coverage, sphinx, flake8, 它們都可以通過  pip 來安裝。之后就可以 make test, make coverage, make docs,make dist 等。其中 make docs 可以生成一個很漂亮的 Web 文檔。

 PyScaffold 創(chuàng)建一個項目

PyScaffold 顧名思義,它是一個用來創(chuàng)建 Python 項目腳手架的工具,安裝和使用: 

  1. $ pip install pyscaffold  
  2. $ putup sample 

這樣創(chuàng)建了一個 Python 項目,目錄結(jié)構(gòu)與前面  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 

整個項目的構(gòu)建就要用 tox 這個工具了。tox 是一個自動化測試和構(gòu)建工具,它在構(gòu)建過程中可創(chuàng)建 Python 虛擬環(huán)境,這讓測試和構(gòu)建能有一個干凈的環(huán)境。

  1. tox -av 能顯示出定義在 tox.ini 中所有的任務(wù):  
  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. 

 要執(zhí)行哪個命令便用 tox -e build, tox -e docs 等, 下面是如何使用 PyScaffold 的動圖:https://yanbin.blog/wp-content/uploads/2021/09/pyscaffold-demo.gif

在我體驗 tox 命令過程中,每一步好像都比較慢,應(yīng)該是創(chuàng)建虛擬機要花些時間。

 PyBuilder

最好再看另一個構(gòu)建工具 PyBuilder, 它所創(chuàng)建出的目錄結(jié)構(gòu)很接近于 Maven, 下面來瞧瞧。

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

完后看下它的目錄結(jié)構(gòu): 

  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 

構(gòu)建過程仍然是用 pyb 命令,可用 pyb -h 查看幫助,pyb -t 列出所有的任務(wù), PyBuilder 的任務(wù)是以插件的方式加入的,插件配置在  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 也是在構(gòu)建或測試之前創(chuàng)建虛擬環(huán)境, 從 0.12.9 版開始可通過參數(shù) --no-venvs 跳過創(chuàng)建虛擬環(huán)境這一步。使用了 --no-venvs 的話 Python 代碼將會在運行  pyb 的當前 Python 環(huán)境中執(zhí)行,所需的依賴將要手工安裝。

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

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

隨后在執(zhí)行 pyb 創(chuàng)建虛擬環(huán)境時就會安裝上面的依賴,并在其中運行測試與構(gòu)建。

 Poetry

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

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

它創(chuàng)建的項目比上面都簡單:

  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 參數(shù),那么源文件目錄 sample 會放在 src 目錄下,即 sample/src/sample。

poetry init 會在當前目錄中生成 pyproject.toml 文件,目錄等的生成需手動完成。

它不關(guān)注文檔的生成,代碼規(guī)范的檢查,代碼覆蓋率都沒有。它的項目配置更集中,全部在 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 文件中定義安裝相應(yīng)的依賴到當前的 Python 虛擬環(huán)境中  
  4.  # 比如在 <test-venv>/lib/python3.9/site-packages 目錄中,安裝好模塊后也可讓測試用例使用  
  5. poetry install        

其他主要的:

  1. 1.  poetry build    # 構(gòu)建可安裝的 *.whl 和 tar.gz 文件  
  2. 2.  poetry shell    # 會根據(jù)定義在 pyproject.toml 文件中的依賴創(chuàng)建并使用虛擬環(huán)境  
  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  # 導(dǎo)出 requirements.txt 文件, --dev  導(dǎo)出含 dev 的依賴,或者用 poetry export --without-hashes > requirements.txt  

poetry run 能執(zhí)行任何系統(tǒng)命令,只是它會在它要的虛擬環(huán)境中執(zhí)行。所以可以想見,poetry 的項目要生成文檔或覆蓋率都必須用 poetry run ... 命令來支持 sphinx, coverage 或 flake8。

在 sample 目錄(與 pyproject.toml 文件平級)中創(chuàng)建文件 my_module.py, 內(nèi)容為:

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

然后在 pyproject.toml 中寫上 

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

再執(zhí)行 

  1. $ poetry run my-script 

就會輸出 "hello poetry"。

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

 

責任編輯:龐桂玉 來源: Python之禪
相關(guān)推薦

2022-08-12 07:56:41

Python項目管理構(gòu)建工具

2023-09-13 11:05:22

物聯(lián)網(wǎng)平臺物聯(lián)網(wǎng)

2020-08-13 10:29:55

項目管理項目經(jīng)理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前端

2018-04-18 21:55:59

多云架構(gòu)云計算數(shù)據(jù)

2022-06-02 08:00:00

數(shù)據(jù)科學機器學習工具

2023-08-22 10:13:53

模塊工具JavaScrip

2024-06-04 22:04:39

2023-03-15 23:59:13

前端構(gòu)建工具

2022-03-25 14:11:11

Java死鎖線程

2025-03-26 01:00:00

2010-02-03 15:09:13

Python 構(gòu)建工具

2020-09-07 14:40:20

Vue.js構(gòu)建工具前端

2024-10-25 15:43:37

2022-07-15 14:54:00

DockerLinux技巧

2021-06-18 06:11:26

工具WebpackSnowpack

2021-11-29 18:02:50

網(wǎng)絡(luò)電纜布線
點贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 亚洲成人一区二区 | 国产视频久久久 | 超碰激情 | 亚洲高清视频在线观看 | 中文字幕一区在线观看视频 | 欧美激情久久久 | 国产高清免费 | 国产在线观看一区二区三区 | 国产日韩欧美二区 | 久久国产精品免费视频 | 欧美二区三区 | 国产偷录视频叫床高潮对白 | 日韩高清一区二区 | 天天草天天操 | 91精品久久久久 | 香蕉婷婷 | 成年人在线视频 | av中文在线 | 精品国产一区二区三区在线观看 | 拍真实国产伦偷精品 | 久久久久久久久久久久久9999 | 精品少妇一区二区三区在线播放 | 国产高清久久 | 成人不卡视频 | 东方伊人免费在线观看 | 天天干天天谢 | 欧美精品一二区 | 成人毛片视频在线播放 | 91玖玖| 亚洲午夜三级 | 91成人免费看 | 亚洲一区二区中文字幕 | 久久久久久久久久一区二区 | 国产欧美一区二区在线观看 | 992人人草 | 香蕉视频黄色 | 亚洲一区二区三区高清 | 免费成人毛片 | 亚洲免费精品 | 久久久久国产 | 毛片一级电影 |