如何在python包上找到反向依赖

时间:2015-01-29 12:20:17

标签: python dependencies package pip

我有一个虚拟环境,其中安装了elasticsearch python包。

我想找到哪个包依赖elasticsearch并在虚拟环境中安装。

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

我尝试了show reverse dependencies with pip?的一些解决方案但是没有用

(.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
[]
>>>

模块返回虚拟环路径的路径。

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

我怀疑这个python包可能会安装elasticsearch debian包,但不确定。

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

1 个答案:

答案 0 :(得分:1)

步骤1.找到您的virtualenv的site-packages目录:

注意我的shell提示符在末尾显示venv38和egrep。

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

site.py模块具有各种有趣的信息,但是我们只对venv的site-package感兴趣。

输出:

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

步骤2。在*dist-info/METADATA文件中查找依赖项

更改为您在上面找到的site-packages目录。

我正在寻找谁在使用bleach而不是elasticsearch

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

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

注意:尽管这里不必担心,但是包名称中的字符-_可能会影响grep的编写方式。

输出:

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

因此,readme_renderer是这种依赖性的源头。

请注意使用find . -name METADATA -exec grep -H Requires-Dist {} \; | grep bleach,即在Requires-Dist和您搜索的包之间交换grep序列的效果不佳,因为在我的情况下,它显示了许多bleach自己的依赖性。