如何以编程方式获取Python包的依赖项列表?
标准的setup.py有文档记录,但是我找不到一种从python或命令行访问它的简单方法。
理想情况下,我要找的是:
$ 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中。
除pip show [package name]命令外,还有pipdeptree命令。
只要做
$ pip install pipdeptree
然后运行
$ pipdeptree
它将以树的形式显示您的依赖关系,例如,
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/pippeptree,在这里您还可以找到使用信息。
pipdeptreeof the dependencies秀在packages installed,not only包装那些of a given。当你可以--json输出滤波器),它仍然depends to be installed on the packages已经。
True is useful but the回答我,当你想得到packages hold of that are not installed为什么是在你的requirements.txt):
你可以使用加-poption to select the only一些你想要的explore packages谁的限制。
尝试在pip中使用show命令,例如:
$ pip show tornado
---
Name: tornado
Version: 4.1
Location: *****
Requires: certifi, backports.ssl-match-hostname
更新(使用指定版本检索DEP):
from pip._vendor import pkg_resources
_package_name = 'somepackage'
_package = pkg_resources.working_set.by_key[_package_name]
print([str(r) for r in _package.requires()]) # retrieve deps from setup.py
Output: ['kombu>=3.0.8',
'billiard>=3.3.0.13',
'boto>=2.26']
你tells that the version of the package,not its just get listed限制;他们。
Requiressee section
是的,但它并不显示"version number the minimum required as the OP:……"
工厂已经packages only for installed。
somehow $ pip3 show beautifulsoup4shows Requires:for empty我不在线beautifulsoup4 depend -什么?
我只是想installing @ xealits,beautifulsoup4在北virtualenv和EN EN depend什么样不在线。
"是的,pythonjin,apparently EN标准packages uses only。我是一位surprised with that。干的好,beautifulsoup4。
亚历克斯的回答很好(+1)。在Python中:
pip._vendor.pkg_resources.working_set.by_key['twisted'].requires()
应该返回类似
[Requirement.parse('zope.interface>=3.6.0')]
其中twisted是包的名称,您可以在字典中找到:
pip._vendor.pkg_resources.WorkingSet().entry_keys
全部列出:
dict = pip._vendor.pkg_resources.WorkingSet().entry_keys
for key in dict:
for name in dict[key]:
req =pip._vendor.pkg_resources.working_set.by_key[name].requires()
print('pkg {} from {} requires {}'.format(name,
key,
req))
应该给您这样的列表:
pkg pyobjc-framework-syncservices from /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC requires [Requirement.parse('pyobjc-core>=2.5.1'), Requirement.parse('pyobjc-framework-Cocoa>=2.5.1'), Requirement.parse('pyobjc-framework-CoreData>=2.5.1')]
上述所有解决方案都是正确的,但有些效率低下。如果您使用的是Mac,最好的方法是使用"pip list"命令。
MacBook Pro:~aimaster$pip列表
希望这有帮助…布雅
在你错过the word编程的问题。
问题就answering the全部……"这些标准documented setup.py has the,but I find an easy to不能接入方式或者Python or from the EN线"命令。
根据本文在python中的说明尝试:
import pip
installed_packages = pip.get_installed_distributions()
installed_packages_list = sorted(["%s==%s" % (i.key, i.version)
for i in installed_packages])
print(installed_packages_list)
它将显示如下:
['behave==1.2.4', 'enum34==1.0', 'flask==0.10.1', 'itsdangerous==0.24',
'jinja2==2.7.2', 'jsonschema==2.3.0', 'markupsafe==0.23', 'nose==1.3.3',
'parse-type==0.3.4', 'parse==1.6.4', 'prettytable==0.7.2', 'requests==2.3.0',
'six==1.6.1', 'vioozer-metadata==0.1', 'vioozer-users-server==0.1',
'werkzeug==0.9.4']
加工好的尝试,它为我。谢谢