show reverse dependencies with pip? show reverse dependencies with pip? python python

show reverse dependencies with pip?


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

import pipdef 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')


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_resourcesimport sysdef 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]))


This is possible for already installed packages using pip's python API. There is the pip.get_installed_distributions function, which can give you a list of all third party packages currently installed.

# rev_deps.pyimport pipimport sysdef find_reverse_deps(package_name):    return [        pkg.project_name for pkg in pip.get_installed_distributions()        if package_name in {req.project_name for req in pkg.requires()}    ]if __name__ == '__main__':    print find_reverse_deps(sys.argv[1])

This script will output the list of packages, that require a specified one:

$python rev_deps.py requests