如何查找Python包的依赖项

问题描述:

如何以编程方式获取Python软件包的依赖关系列表?

How can you programmatically get a Python package's list of dependencies?

标准setup.py已记录了这些内容,但我找不到从Python或命令行从 访问它的简便方法.

The standard setup.py has these documented, but I can't find an easy way to access it from either Python or the command line.

理想情况下,我正在寻找类似的东西:

Ideally, I'm looking for something like:

$ pip install somepackage --only-list-deps
kombu>=3.0.8
billiard>=3.3.0.13
boto>=2.26

或:

>>> import package_deps
>>> package = package_deps.find('somepackage')
>>> print package.dependencies
['kombu>=3.0.8', 'billiard>=3.3.0.13', 'boto>=2.26']

注意,我不是在谈论导入软件包和查找所有引用的模块.尽管这可能找到大多数相关软件包,但无法找到所需的最低版本号.那只存储在setup.py中.

Note, I'm not talking about importing a package and finding all referenced modules. While this might find most of the dependent packages, it wouldn't be able to find the minimum version number required. That's only stored in the setup.py.

除了pip show [package name]命令之外,还有pipdeptree.

In addition to the pip show [package name] command, there is pipdeptree.

就做

$ pip install pipdeptree

然后运行

$ pipdeptree

,它将以树形式显示您的依赖关系,例如

and it will show you your dependencies in a tree form, e.g.,

flake8==2.5.0
  - mccabe [required: >=0.2.1,<0.4, installed: 0.3.1]
  - pep8 [required: !=1.6.0,>=1.5.7,!=1.6.1,!=1.6.2, installed: 1.5.7]
  - pyflakes [required: >=0.8.1,<1.1, installed: 1.0.0]
ipdb==0.8
  - ipython [required: >=0.10, installed: 1.1.0]

该项目位于 https://github.com/naiquevin/pipdeptree ,您将在其中还可以找到使用情况信息.

The project is located at https://github.com/naiquevin/pipdeptree, where you will also find usage information.