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
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:
--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. Using just the -p option without --json-tree works as expected:
$ 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:
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