Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

show reverse dependencies with pip?

Tags:

Is it possible to show the reverse dependencies with pip?

I want to know which package needs package foo. And which version of foo is needed by this package.

like image 738
guettli Avatar asked Jan 24 '14 15:01

guettli


People also ask

How do I see dependency on pip?

Pip Check Command – Check Python Dependencies After Installation. Because pip doesn't currently address dependency issues on installation, the pip check command option can be used to verify that dependencies have been installed properly in your project. For example: $ pip check No broken requirements found.

How do I resolve dependencies in pip?

Unfortunately, pip makes no attempt to resolve dependency conflicts. For example, if you install two packages, package A may require a different version of a dependency than package B requires. Pip can install from either Source Distributions (sdist) or Wheel (. whl) files.

Does pip do dependency resolution?

Pip does not provide true dependency resolution, but this can be solved by using it in conjunction with a requirements. txt file. Requirements. txt files can be used to make pip resolve dependency conflicts between different packages.

What are reverse dependencies?

A reverse dependency – meaning another package depends on the existence of your package Note – Use the reverse dependency type only when a package that cannot deliver a depend file relies on your package. An incompatible package – meaning your package is incompatible with the named package.


2 Answers

I found Alexander's answer perfect, except it's hard to copy/paste. Here is the same, ready to paste:

import pip def rdeps(package_name):     return [pkg.project_name             for pkg in pip.get_installed_distributions()             if package_name in [requirement.project_name                                 for requirement in pkg.requires()]]  rdeps('some-package-name') 
like image 143
Wernight Avatar answered Nov 01 '22 20:11

Wernight


To update the answer to current (2019), when pip.get_installed_distributions() does not exist anymore, use pkg_resources (as mentioned in a comments):

import pkg_resources import sys  def find_reverse_deps(package_name):     return [         pkg.project_name for pkg in pkg_resources.WorkingSet()         if package_name in {req.project_name for req in pkg.requires()}     ]  if __name__ == '__main__':     print(find_reverse_deps(sys.argv[1])) 
like image 45
9769953 Avatar answered Nov 01 '22 21:11

9769953