Python 中的包或模块具有依赖关系。有时候离线安装时总会提示缺少依赖包,此时就需要知道安装的模块依赖哪些包。然后下载相应的模块即可。那如何查看某个模块的依赖包有哪些呢?本篇介绍一些方法。

使用 pip

如果模块是通过 pip 安装的,而不是通过 conda 安装的,可以使用如下的方法查看

1
pip show visdom

结果大概是这样

1
2
3
4
5
6
7
8
9
10
Name: visdom
Version: 0.1.8.9
Summary: A tool for visualizing live, rich data for Torch and Numpy
Home-page: https://github.com/facebookresearch/visdom
Author: Jack Urbanek, Allan Jabri, Laurens van der Maaten
Author-email: jju@fb.com
License: CC-BY-NC-4.0
Location: /usr/local/miniconda/lib/python3.9/site-packages
Requires: pyzmq, scipy, requests, tornado, six, torchfile, jsonpatch, websocket-client, pillow, numpy
Required-by:

从上面可以清楚的看到模块 visdom 的依赖模块有 pyzmq, scipy, requests, tornado, six, torchfile, jsonpatch, websocket-client, pillow, numpy

使用 pipdeptree

直接使用 pip 查看能够简单的看到依赖包,使用 pipdeptree 模块能够方便的查看更详细的依赖关系。不过,在这之前,需要先安装该模块

1
pip install pipdeptree

查看 visdom 的依赖关系

1
pipdeptree -p visdom

结果大概如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
visdom==0.1.8.9
- jsonpatch [required: Any, installed: 1.32]
- jsonpointer [required: >=1.9, installed: 2.3]
- numpy [required: >=1.8, installed: 1.22.4]
- pillow [required: Any, installed: 9.1.1]
- pyzmq [required: Any, installed: 23.2.0]
- requests [required: Any, installed: 2.27.1]
- certifi [required: >=2017.4.17, installed: 2022.6.15]
- charset-normalizer [required: ~=2.0.0, installed: 2.0.4]
- idna [required: >=2.5,<4, installed: 3.3]
- urllib3 [required: >=1.21.1,<1.27, installed: 1.26.9]
- scipy [required: Any, installed: 1.8.1]
- numpy [required: >=1.17.3,<1.25.0, installed: 1.22.4]
- six [required: Any, installed: 1.16.0]
- torchfile [required: Any, installed: 0.1.0]
- tornado [required: Any, installed: 6.1]
- websocket-client [required: Any, installed: 1.3.3]

从上面的结果可以看出,依赖关系更加详细,不仅能查看一级依赖还能查看二级依赖,同时,能够看到依赖的包的版本要求。

更多关于 pipdeptree 的使用,可以使用如下方法查看

1
pipdeptree -h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
usage: pipdeptree [-h] [-v] [-f] [--python PYTHON] [-a] [-l] [-u] [-w [{silence,suppress,fail}]] [-r] [-p PACKAGES] [-e PACKAGES] [-j]
[--json-tree] [--graph-output OUTPUT_FORMAT]

Dependency tree of the installed python packages

optional arguments:
-h, --help show this help message and exit
-v, --version show program's version number and exit
-f, --freeze Print names so as to write freeze files
--python PYTHON Python to use to look for packages in it (default: where installed)
-a, --all list all deps at top level
-l, --local-only If in a virtualenv that has global access do not show globally installed packages
-u, --user-only Only show installations in the user site dir
-w [{silence,suppress,fail}], --warn [{silence,suppress,fail}]
Warning control. "suppress" will show warnings but return 0 whether or not they are present. "silence" will not
show warnings at all and always return 0. "fail" will show warnings and return 1 if any are present. The
default is "suppress".
-r, --reverse Shows the dependency tree in the reverse fashion ie. the sub-dependencies are listed with the list of packages
that need them under them.
-p PACKAGES, --packages PACKAGES
Comma separated list of select packages to show in the output. If set, --all will be ignored.
-e PACKAGES, --exclude PACKAGES
Comma separated list of select packages to exclude from the output. If set, --all will be ignored.
-j, --json Display dependency tree as json. This will yield "raw" output that may be used by external tools. This option
overrides all other options.
--json-tree Display dependency tree as json which is nested the same way as the plain text output printed by default. This
option overrides all other options (except --json).
--graph-output OUTPUT_FORMAT
Print a dependency graph in the specified output format. Available are all formats supported by GraphViz, e.g.:
dot, jpeg, pdf, png, svg

参考文献

  1. python 查看某个第三方包的依赖包和被依赖包以及指定pip源安装

  2. 查看已安装的Python包依赖树_赵成永的博客-CSDN博客_pip 查看包的依赖