你可能時(shí)常會(huì)遇到由于包的版本不匹配導(dǎo)致代碼報(bào)錯(cuò)的問題,由于 pip freeze 將所有依賴項(xiàng)顯示為二維列表,這時(shí)候如果想找到這個(gè)錯(cuò)誤版本的包是比較麻煩的事情。這時(shí)候,有個(gè)工具你必須得知道,它就是 pipdeptree .
pipdeptree 是一個(gè)命令行實(shí)用程序,它能用于以依賴關(guān)系樹可視化的形式顯示已安裝的python包。
它適用于全局安裝在計(jì)算機(jī)上的各個(gè)模塊,也適用于Virtualenv等虛擬環(huán)境中的模塊。
1.安裝
你只需要在你的環(huán)境中輸入以下命令就能安裝 pipdeptree:
pip install pipdeptree
已通過測試的Python版本:2.7,3.5,3.6,3.7,3.8,3.9.
2.用法和示例
pip freeze 和 pipdeptree 最大的區(qū)別如下:
# pip freeze 的顯示
$ pip freeze
Flask==0.10.1
itsdangerous==0.24
Jinja2==2.11.2
-e git+git@github.com:naiquevin/lookupy.git@cdbe30c160e1c29802df75e145ea4ad903c05386#egg=Lookupy
MarkupSafe==0.22
pipdeptree @ file:///private/tmp/pipdeptree-2.0.0b1-py3-none-any.whl
Werkzeug==0.11.2
可見,pip freeze 最多只能顯示一個(gè)依賴的列表,而在 pipdeptree ,每個(gè)模塊的依賴關(guān)系能夠非常直觀地展示出來:
$ pipdeptree
Warning!!! Possibly conflicting dependencies found:
* Jinja2==2.11.2
- MarkupSafe [required: >=0.23, installed: 0.22]
------------------------------------------------------------------------
Flask==0.10.1
- itsdangerous [required: >=0.21, installed: 0.24]
- Jinja2 [required: >=2.4, installed: 2.11.2]
- MarkupSafe [required: >=0.23, installed: 0.22]
- Werkzeug [required: >=0.7, installed: 0.11.2]
Lookupy==0.1
pipdeptree==2.0.0b1
- pip [required: >=6.0.0, installed: 20.1.1]
setuptools==47.1.1
wheel==0.34.2
請(qǐng)注意這個(gè) Warning,提示了你哪些模塊會(huì)造成其依賴的模塊版本發(fā)生沖突,這是非常有用的提示,很多時(shí)候問題就出現(xiàn)在這里。
不僅如此,如果存在循環(huán)性依賴,比如:
**CircularDependencyA => CircularDependencyB => CircularDependencyA
**
它會(huì)進(jìn)行如下提示:
$ pipdeptree --exclude pip,pipdeptree,setuptools,wheel
Warning!!! Cyclic dependencies found:
- CircularDependencyA = > CircularDependencyB = > CircularDependencyA
- CircularDependencyB = > CircularDependencyA = > CircularDependencyB
------------------------------------------------------------------------
wsgiref==0.1.2
argparse==1.2.1
如果你想生成 requirements.txt,可以這么做:
$ pipdeptree -f | tee locked-requirements.txt
Flask==0.10.1
itsdangerous==0.24
Jinja2==2.11.2
MarkupSafe==0.23
Werkzeug==0.11.2
gnureadline==8.0.0
-e git+git@github.com:naiquevin/lookupy.git@cdbe30c160e1c29802df75e145ea4ad903c05386#egg=Lookupy
pipdeptree @ file:///private/tmp/pipdeptree-2.0.0b1-py3-none-any.whl
pip==20.1.1
setuptools==47.1.1
wheel==0.34.2
在確認(rèn)沒有沖突的依賴項(xiàng)后,甚至可以將其“鎖定”,其中所有包都將固定到其當(dāng)前安裝的版本:
$ pipdeptree -f | sed 's/ //g' | sort -u > locked-requirements.txt
3. 可視化依賴樹
為了能夠可視化展示依賴樹,我們需要安裝GraphViz,安裝GraphViz的教程可見這篇文章:Python 一鍵轉(zhuǎn)化代碼為流程圖。安裝完成后輸入以下命令:
pipdeptree --graph-output png > dependencies.png
# pipdeptree --graph-output dot > dependencies.dot
# pipdeptree --graph-output pdf > dependencies.pdf
# pipdeptree --graph-output svg > dependencies.svg
支持四種格式的輸出,這里png的輸出效果如下:
效果是非常不錯(cuò)的,大家如果有需要清理依賴的大型項(xiàng)目,可以用 pipdeptree 試一下。
-
模塊
+關(guān)注
關(guān)注
7文章
2788瀏覽量
50359 -
代碼
+關(guān)注
關(guān)注
30文章
4900瀏覽量
70713 -
虛擬環(huán)境
+關(guān)注
關(guān)注
0文章
29瀏覽量
9058 -
python
+關(guān)注
關(guān)注
56文章
4827瀏覽量
86708
發(fā)布評(píng)論請(qǐng)先 登錄
Python項(xiàng)目的依賴管理
Linux操作系統(tǒng)是同樣需要清理工具的
大型項(xiàng)目外包合作
想學(xué)labview異步多線程,大型項(xiàng)目規(guī)范,各類通訊的找我
用labview寫大型項(xiàng)目時(shí),應(yīng)該用什么架構(gòu)寫能利于調(diào)試和維護(hù)?
面向大型項(xiàng)目組織的Web工作流集成框架
管理與開發(fā)LabVIEW大型項(xiàng)目
如何在代碼編寫器Studio中處理大型項(xiàng)目詳細(xì)概述

區(qū)塊鏈在發(fā)展中國家設(shè)施大型項(xiàng)目中的意義
c盤清理工具
在大型項(xiàng)目中使用Cscope
Google刪除Chrome清理工具
大型Python項(xiàng)目依賴樹如何一鍵理清

Kondo-為開發(fā)者設(shè)計(jì)的磁盤清理工具

評(píng)論