Neel
Neel

Reputation: 21243

How to find reverse dependency on python package

I have one virtual environment, where elasticsearch python package was installed.

I want to find, which package has dependency on elasticsearch and did installation in virtual environment.

(.venv)root@test:~# pip freeze | grep elast
elasticsearch==1.4.0.dev0

I tried some solution from show reverse dependencies with pip? but its not worked

(.venv)root@test:~# python
Python 2.7.8 (default, Oct 18 2014, 12:50:18)
[GCC 4.9.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pip
>>> package_name = 'elasticsearch'
>>> rev_dep = [pkg.project_name for pkg in pip.get_installed_distributions() if package_name in [requirement.project_name for requirement in pkg.requires()]]
>>> rev_dep
[]
>>>

Path of module return virtual env path.

(.venv)root@test:~# python -c 'import elasticsearch; print elasticsearch.__path__'
['/opt/venvs/.venv/local/lib/python2.7/site-packages/elasticsearch']

I have doubt that elasticsearch debian package might be installed this python package, but not sure.

(.venv)root@test:~# dpkg -l | grep elast
ii  elasticsearch                        1.2.0                           all          Open Source, Distributed, RESTful Search Engine

Upvotes: 3

Views: 1630

Answers (1)

JL Peyret
JL Peyret

Reputation: 12144

Step 1. find your site-packages directory for your virtualenv:

Note my shell prompt showing venv38 and the egrep at the end.

(venv38) myuser@foo$ python -m site | egrep venv38

The site.py module has all sorts of interesting info, but we are only interested in the venv's site-package.

Output:

'/Users/myuser/kds2/py2/venv38/lib/python3.8/site-packages',

Step 2. look for dependencies in the *dist-info/METADATA files

change to the site-packages directory you found above.

I am looking for who is using bleach rather than elasticsearch

cd /Users/myuser/kds2/py2/venv38/lib/python3.8/site-packages

find . -name METADATA -exec grep -H -i bleach {} \; | grep Requires-Dist

Note: although it's not necessary to worry about it here, characters like - or _ in the package name may affect how the grep should be written.

output:

./readme_renderer-24.0.dist-info/METADATA:Requires-Dist: bleach (>=2.1.0)

So, the readme_renderer is what is pulling in this dependency.

Note using find . -name METADATA -exec grep -H Requires-Dist {} \; | grep bleach i.e. interchanging the grep sequence between Requires-Dist and your searched-for package doesn't work as well, as, in my case, it showed a lot of bleachs own dependencies.

Upvotes: 2

Related Questions