如何查找 Python 包的依赖项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29751572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find a Python package's dependencies
提问by Cerin
How can you programmatically get a Python package's list of dependencies?
如何以编程方式获取 Python 包的依赖项列表?
The standard setup.py
has these documented, but I can't find an easy way to access it fromeither Python or the command line.
该标准setup.py
已记录这些内容,但我找不到从Python 或命令行访问它的简单方法。
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.
请注意,我不是在谈论导入包并查找所有引用的模块。虽然这可能会找到大多数依赖包,但它无法找到所需的最低版本号。这仅存储在 setup.py 中。
回答by Alex Lisovoy
Try to use show
command in pip
, for example:
尝试在 中使用show
命令pip
,例如:
$ pip show tornado
---
Name: tornado
Version: 4.1
Location: *****
Requires: certifi, backports.ssl-match-hostname
Update(retrieve deps with specified version):
更新(检索指定版本的 deps):
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']
回答by cgseller
(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 版本应该避免使用,留在这里以供参考旧 PIP 版本)Alex 的答案很好 (+1)。在蟒蛇中:
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 :
其中twisted 是包的名称,您可以在字典中找到:
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')]
回答by beruic
In addition to the pip show [package name]
command, there is pipdeptree
.
除了pip show [package name]
命令之外,还有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.
该项目位于https://github.com/naiquevin/pipdeptree,您还可以在其中找到使用信息。
回答by EniGma
Try this according to this articlein python:
根据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']
回答by Jordan Mackie
Quite a few answers here show pip being imported for use in programs. The documentation for pip strongly advises against this usage of pip.
这里有相当多的答案显示 pip 被导入以用于程序。pip的文档强烈建议不要使用 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) .
而不是pkg_resources
通过 pip 导入访问,您实际上可以pkg_resources
直接导入并使用相同的逻辑(这实际上是 pip 文档中为任何想要以编程方式查看包元信息的人链接的建议解决方案之一)。
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 :)
如果你有一些麻烦,找出你的包的名字是什么,将WorkingSet
通过返回的实例pkg_resources.working_set
工具__iter__
,所以你可以打印所有这些,希望发现在那里你:)
i.e.
IE
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)
这适用于 python 2 和 3(尽管您需要调整 python2 的打印语句)
回答by Praboda
Use https://libraries.io/. It is a good place to explore dependencies before installing using pip.
使用https://libraries.io/。这是在使用 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.
例如。输入 google-cloud-storage 并搜索,然后您可以找到该库的页面 ( https://libraries.io/rubygems/google-cloud-storage)。从“版本”(默认为最新)中选择您要探索其依赖项的版本,在“依赖项”下,您可以找到依赖项列表及其支持的版本。