How can I see all packages that depend on a certai

2020-02-24 13:10发布

问题:

I would like to see a list of packages that depend on a certain package with PIP. That is, given django, I would like to see django-cms, django-filer, because I have these packages installed and they all have django as dependency.

回答1:

Quite straightforward:

pip show <insert_package_name_here>| grep ^Requires

Or the other way around: (sorry i got it wrong!)

for NAME in $(pip freeze | cut -d= -f1); do REQ=$(pip show $NAME| grep Requires); if [[ "$REQ" =~ "$REQUIRES" ]]; then echo $REQ;echo "Package: $NAME"; echo "---" ; fi;  done

before that set your search-string with:

REQUIRES=django

essentially you have to go through the whole list and query for every single one. That may take some time.


Edit: Also it does only work on installed packages, I don't see pip providing dependencies on not installed packages.



回答2:

I know there's already an accepted answer here, but really, it seems to me that what you want is to use pipdeptree:

pip install pipdeptree
pipdeptree --help

pipdeptree -r -p django


回答3:

This one, for pip older than 1.3.1 will list all packages and it's dependencies, you can parse its output with any scripting language, for Requires ... django inclusions:

pip freeze | cut -f 1 -d'=' |  xargs -L1 pip show 

For example, following snippet:

import os
import re

package = 'numpy'
regex = re.compile('.*{}($|,).*'.format(package))

def chunks(l, n): return [l[i:i+n] for i in range(0, len(l), n)]

cmd = "pip freeze | cut -f 1 -d'=' |  xargs -L1 pip show"
packages = os.popen(cmd).read()
pkg_infos = chunks(packages.splitlines(), 5)
print '\n'.join(x[1][6:] for x in filter(lambda x: regex.match(x[-1]), pkg_infos))

outputs pandas on my system.



回答4:

One liner based on requirements.txt. In this example I'm looking for funcsigs reverse dependency, and found mock. Just change funcsigs by something else.

cat requirements.txt | grep -v git | sed 's/==.*//' | xargs -I % echo 'pip show % 2>/dev/null | grep Requires | grep -q funcsigs && echo %' | sh