简体   繁体   English

获取python中特定程序包的依赖树

[英]Get dependency tree of a particular package in python

I want to get dependencies of a particular package. 我想获取特定程序包的依赖关系。 Using below command I get the complete dependency tree showing all the installed libraries. 使用以下命令,我得到了完整的依赖关系树,其中显示了所有已安装的库。

pipdeptree --json-tree -p numpy pipdeptree --json-tree -p numpy

Is there a way to get only the dependencies of a package in tree form 有没有一种方法可以仅获取树形式的程序包的依赖项

According to the help in pipdeptree -h , the --json-tree option overrides the -p option: 根据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).

So, unfortunately, it looks like showing the tree of a single package as json is not normally possible. 因此,不幸的是,看起来像显示单个包的树,因为通常无法实现json。 Using just the -p option without --json-tree works as expected: 仅使用-p选项而不使用--json-tree可以按预期工作:

$ pipdeptree -p numpy
numpy==1.16.2

But unfortunately that is just regular output. 但不幸的是,这只是常规输出。

Of course you could always hack it together by importing pipdeptree into a script: 当然,您总是可以通过将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])

outputs 输出

{'required_version': '1.16.2', 'dependencies': [], 'package_name': 'numpy', 'installed_version': '1.16.2', 'key': 'numpy'}

The source code is here if you want to try something like this: https://github.com/naiquevin/pipdeptree/blob/master/pipdeptree.py 如果您想尝试这样的操作,则源代码在这里: https : //github.com/naiquevin/pipdeptree/blob/master/pipdeptree.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM