1

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

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Even I thought to do the same but its kind of time consuming to find the package in the tree and getting dependencies if we are looking for large no. of package versions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.