[英]Get dependency tree of a particular package in python
我想获取特定程序包的依赖关系。 使用以下命令,我得到了完整的依赖关系树,其中显示了所有已安装的库。
pipdeptree --json-tree -p numpy
有没有一种方法可以仅获取树形式的程序包的依赖项
根据pipdeptree -h
的帮助,-- --json-tree
选项将覆盖-p
选项:
--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).
因此,不幸的是,看起来像显示单个包的树,因为通常无法实现json。 仅使用-p
选项而不使用--json-tree
可以按预期工作:
$ pipdeptree -p numpy
numpy==1.16.2
但不幸的是,这只是常规输出。
当然,您总是可以通过将pipdeptree导入脚本中来将其合并在一起:
import pipdeptree
import json
pkgs = pipdeptree.get_installed_distributions()
dist_index = pipdeptree.build_dist_index(pkgs)
tree = pipdeptree.construct_tree(dist_index)
json_tree = json.loads(pipdeptree.render_json_tree(tree, indent=0))
print([package for package in json_tree if package['package_name'] == 'numpy'][0])
输出
{'required_version': '1.16.2', 'dependencies': [], 'package_name': 'numpy', 'installed_version': '1.16.2', 'key': 'numpy'}
如果您想尝试这样的操作,则源代码在这里: https : //github.com/naiquevin/pipdeptree/blob/master/pipdeptree.py
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.