问题:如何查找Python包的依赖项
如何以编程方式获取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中。
How can you programmatically get a Python package’s list of dependencies?
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
or:
>>> import package_deps
>>> package = package_deps.find('somepackage')
>>> print package.dependencies
['kombu>=3.0.8', 'billiard>=3.3.0.13', 'boto>=2.26']
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.
回答 0
除了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/pipdeptree,您还将在其中找到使用信息。
In addition to the pip show [package name]
command, there is pipdeptree
.
Just do
$ pip install pipdeptree
then run
$ 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]
The project is located at https://github.com/naiquevin/pipdeptree, where you will also find usage information.
回答 1
尝试在中使用show
命令pip
,例如:
$ pip show tornado
---
Name: tornado
Version: 4.1
Location: *****
Requires: certifi, backports.ssl-match-hostname
更新(检索指定版本的部门):
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']
Try to use show
command in pip
, for example:
$ pip show tornado
---
Name: tornado
Version: 4.1
Location: *****
Requires: certifi, backports.ssl-match-hostname
Update (retrieve deps with specified version):
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']
回答 2
这里有很多答案,表明将pip导入以便在程序中使用。pip的文档强烈建议不要使用pip。
实际上pkg_resources
,您可以pkg_resources
直接导入并使用相同的逻辑(而不是通过pip导入进行访问)(这实际上是pip docs中链接的建议解决方案之一,适用于希望以编程方式查看程序包元信息的任何人)。
import pkg_resources
_package_name = 'yourpackagename'
def get_dependencies_with_semver_string():
package = pkg_resources.working_set.by_key[_package_name]
return [str(r) for r in package.requires()]
如果您在查找确切的软件包名称时遇到麻烦,可以WorkingSet
通过pkg_resources.working_set
Implements 返回的实例,__iter__
以便将它们全部打印出来,并希望在其中找到它们:)
即
import pkg_resources
def print_all_in_working_set():
ws = pkg_resources.working_set
for package_name in ws:
print(ws)
这适用于python 2和python 3(尽管您需要调整python2的打印语句)
Quite a few answers here show pip being imported for use in programs. The documentation for pip strongly advises against this usage of pip.
Instead of accessing pkg_resources
via the pip import, you can actually just import pkg_resources
directly and use the same logic (which is actually one of the suggested solutions in the pip docs linked for anyone wanting to see package meta information programmatically) .
import pkg_resources
_package_name = 'yourpackagename'
def get_dependencies_with_semver_string():
package = pkg_resources.working_set.by_key[_package_name]
return [str(r) for r in package.requires()]
If you’re having some trouble finding out exactly what your package name is, the WorkingSet
instance returned by pkg_resources.working_set
implements __iter__
so you can print all of them and hopefully spot yours in there :)
i.e.
import pkg_resources
def print_all_in_working_set():
ws = pkg_resources.working_set
for package_name in ws:
print(ws)
This works with both python 2 and 3 (though you’ll need to adjust the print statements for python2)
回答 3
(这是一个传统的答案,应该避免使用现代PIP版本,并在此引用旧PIP版本)。Alex的回答很好(+1)。在python中:
pip._vendor.pkg_resources.working_set.by_key['twisted'].requires()
应该返回类似
[Requirement.parse('zope.interface>=3.6.0')]
包装的名称在哪里是扭曲的,您可以在字典中找到它:
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')]
(THIS IS A LEGACY ANSWER AND SHOULD BE AVOIDED FOR MODERN PIP VERSIONS AND LEFT HERE FOR REFERENCE TO OLDER PIP VERSIONS )
Alex’s answer is good (+1). In python:
pip._vendor.pkg_resources.working_set.by_key['twisted'].requires()
should return something like
[Requirement.parse('zope.interface>=3.6.0')]
where twisted is the name of the package, which you can find in the dictionary :
pip._vendor.pkg_resources.WorkingSet().entry_keys
to list them all:
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))
should give you lists like this:
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')]
回答 4
Use https://libraries.io/. It is a good place to explore dependencies before installing using pip.
Eg. Type google-cloud-storage and search, then you can find the page for the library (https://libraries.io/rubygems/google-cloud-storage). Select the version for which you want to explore the dependencies from the ‘Releases’ (default is the latest), Under ‘Dependencies’ you can find the dependency list and their supported versions.
回答 5
根据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']
Try this according to this article in 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)
It will show like:
['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']