用pip显示反向依赖关系?

gue*_*tli 14 python dependencies pip

是否可以显示反向依赖关系pip

我想知道哪个包需要包foo.foo这个软件包需要哪个版本.

Wer*_*ght 15

我发现Alexander的答案很完美,除了很难复制/粘贴.这是相同的,准备粘贴:

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')
Run Code Online (Sandbox Code Playgroud)

  • 为了记录,pip没有公共API.pip 10的答案无效.请使用setuptools中的`pkg_resources`. (2认同)

Ale*_*kov 11

对于使用pip的python API的已安装软件包,这是可行的.有一个pip.get_installed_distributions功能,它可以为您提供当前安装的所有第三方软件包的列表.

# rev_deps.py
import pip
import sys

def 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])
Run Code Online (Sandbox Code Playgroud)

此脚本将输出需要指定的包列表:

$python rev_deps.py requests
Run Code Online (Sandbox Code Playgroud)


0 0*_*0 0 9

要更新当前 (2019) 的答案,当pip.get_installed_distributions()不再存在时,请使用pkg_resources(如评论中所述):

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]))
Run Code Online (Sandbox Code Playgroud)


x-y*_*uri 7

可以使用该pipdeptree包。列出已安装cffi包的反向依赖关系:

$ pipdeptree -p cffi -r
cffi==1.14.0
  - cryptography==2.9 [requires: cffi>=1.8,!=1.11.3]
    - social-auth-core==3.3.3 [requires: cryptography>=1.4]
      - python-social-auth==0.3.6 [requires: social-auth-core]
      - social-auth-app-django==2.1.0 [requires: social-auth-core>=1.2.0]
Run Code Online (Sandbox Code Playgroud)