在 Python 的項目中,如何管理所用的所有依賴庫呢?最主流的作法是維護一份「requirements.txt」,記錄下依賴庫的名字及其版本號。python
那麼,如何來生成這份文件呢?在上篇文章《由淺入深:Python 中如何實現自動導入缺失的庫?》中,我提到了一種常規的方法:git
pip freeze > requirements.txt
這種方法用起來方便,但有幾點不足:github
可用於項目依賴管理的工具備不少,本文主要圍繞與 requirements.txt 文件相關的、比較類似卻又各具特點的 4 個三方庫,簡要介紹它們的使用方法,羅列一些顯著的功能點。至於哪一個是最好的管理方案呢?賣個關子,請往下看……正則表達式
這是個很受歡迎的用於管理項目中依賴庫的工具,能夠用「pip install pipreqs」命令來安裝。它的主要特色有:json
基本的命令選項以下:flask
Usage: pipreqs [options] <path> Options: --use-local Use ONLY local package info instead of querying PyPI --pypi-server <url> Use custom PyPi server --proxy <url> Use Proxy, parameter will be passed to requests library. You can also just set the environments parameter in your terminal: $ export HTTP_PROXY="http://10.10.1.10:3128" $ export HTTPS_PROXY="https://10.10.1.10:1080" --debug Print debug information --ignore <dirs>... Ignore extra directories --encoding <charset> Use encoding parameter for file open --savepath <file> Save the list of requirements in the given file --print Output the list of requirements in the standard output --force Overwrite existing requirements.txt --diff <file> Compare modules in requirements.txt to project imports. --clean <file> Clean up requirements.txt by removing modules that are not imported in project.
其中需注意,極可能遇到編碼錯誤:UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in
。須要指定編碼格式「--encoding=utf8」。工具
在已生成依賴文件「requirements.txt」的狀況下,它能夠強行覆蓋、比對差別以及清除再也不使用的依賴項。測試
pigar 一樣能夠根據項目路徑來生成依賴文件,並且會列出依賴庫在文件中哪些位置使用到了。這個功能充分利用了 requirements.txt 文件中的註釋,能夠提供很豐富的信息。ui
pigar 對於查詢真實的導入源頗有幫助,例如bs4
模塊來自beautifulsoup4
庫,MySQLdb
則來自於MySQL_Python
庫。能夠經過「-s」參數,查找真實的依賴庫。編碼
$ pigar -s bs4 MySQLdb
它使用解析 AST 的方式,而非正則表達式的方式,能夠很方便地從 exec/eval 的參數、文檔字符串的文檔測試中提取出依賴庫。
另外,它對於不一樣 Python 版本的差別能夠很好地支持。例如,concurrent.futures
是 Python 3.2+ 的標準庫,而在以前早期版本中,須要安裝三方庫futures
,才能使用它。pigar 作到了有效地識別區分。(PS:pipreqs 也支持這個識別,詳見這個合入:https://github.com/bndr/pipreqs/pull/80)
pip-tools 包含一組管理項目依賴的工具:pip-compile 與 pip-sync,可使用命令「pip install pip-tools」統一安裝。它最大的優點是能夠精準地控制項目的依賴庫。
兩個工具的用途及關係圖以下:
pip-compile 命令主要用於生成依賴文件和升級依賴庫,另外它能夠支持 pip 的「Hash-Checking Mode 」,並支持在一個依賴文件中嵌套其它的依賴文件(例如,在 requirements.in 文件內,能夠用「-c requirements.txt」方式,引入一個依賴文件)。
它能夠根據 setup.py 文件來生成 requirements.txt,假如一個 Flask 項目的 setup.py 文件中寫了「install_requires=['Flask']」,那麼能夠用命令來生成它的全部依賴:
$ pip-compile # # This file is autogenerated by pip-compile # To update, run: # # pip-compile --output-file requirements.txt setup.py # click==6.7 # via flask flask==0.12.2 itsdangerous==0.24 # via flask jinja2==2.9.6 # via flask markupsafe==1.0 # via jinja2 werkzeug==0.12.2 # via flask
在不使用 setup.py 文件的狀況下,能夠建立「requirements.in」,在裏面寫入「Flask」,再執行「pip-compile requirements.in」,能夠達到跟前面同樣的效果。
pip-sync 命令能夠根據 requirements.txt 文件,來對虛擬環境中進行安裝、升級或卸載依賴庫(注意:除了 setuptools、pip 和 pip-tools 以外)。這樣能夠有針對性且按需精簡地管理虛擬環境中的依賴庫。
另外,該命令能夠將多個「*.txt」依賴文件歸併成一個:
$ pip-sync dev-requirements.txt requirements.txt
它的主要用途是展現 Python 項目的依賴樹,經過有層次的縮進格式,顯示它們的依賴關係,不像前面那些工具只會生成扁平的並列關係。
除此以外,它還能夠:
它也有缺點,好比沒法穿透虛擬環境。若是要在虛擬環境中工做,必須在該虛擬環境中安裝 pipdeptree。由於跨虛擬環境會出現重複或衝突等狀況,所以須要限定虛擬環境。可是每一個虛擬環境都安裝一個 pipdeptree,仍是挺讓人難受的。
好啦,4 種庫介紹完畢,它們的核心功能都是分析依賴庫,生成 requirements.txt 文件,同時,它們又具備一些差別,補齊了傳統的 pip 的某些不足。
本文不對它們做全面的測評,只是選取了一些主要特性進行介紹,好在它們安裝方便(pip install xxx),使用也簡單,感興趣的同窗不妨一試。
更多豐富的細節,請查閱官方文檔:
https://github.com/bndr/pipreqs
https://github.com/damnever/pigar
https://github.com/jazzband/pip-tools
https://github.com/naiquevin/pipdeptree
公衆號【Python貓】, 本號連載優質的系列文章,有喵星哲學貓系列、Python進階系列、好書推薦系列、技術寫做、優質英文推薦與翻譯等等,歡迎關注哦。</file></file></file></charset></dirs></url></url></path>