如何找到对python包的反向依赖 [英] How to find reverse dependency on python package
问题描述
我有一个虚拟环境,其中安装了 elasticsearch
python软件包。
I have one virtual environment, where elasticsearch
python package was installed.
我要查找的哪个软件包依赖 elasticsearch
并在虚拟环境中进行了安装。
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
我尝试了显示带有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
[]
>>>
模块返回虚拟环境路径的路径。
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']
我怀疑 elasticsearch
debian软件包是否可以安装此python软件包,但不确定。
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
推荐答案
步骤1.找到您的virtualenv的站点软件包目录:
请注意我的shell提示符显示 venv38 $ c $
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
site.py模块包含各种有趣的信息,但我们只对venv的站点包感兴趣。
The site.py module has all sorts of interesting info, but we are only interested in the venv's site-package.
'/Users/myuser/kds2/py2/venv38/lib/python3.8/site-packages',
步骤2。在 * dist-info / METADATA $ c $中查找依赖项c>文件
更改为您在上面找到的site-packages目录。
Step 2. look for dependencies in the *dist-info/METADATA
files
change to the site-packages directory you found above.
我正在寻找谁正在使用漂白剂
而不是 elasticsearch
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
注意:尽管这里不必担心,但<$ c包名称中的$ c>-或 _
可能会影响grep的编写方式。
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.
./readme_renderer-24.0.dist-info/METADATA:Requires-Dist: bleach (>=2.1.0)
因此, readme_renderer
是这种依赖性的源头。
So, the readme_renderer
is what is pulling in this dependency.
请注意使用 find。 -name METADATA -exec grep -H要求-Dist {} \; | grep bleach
,即在Requires-Dist和您搜索的包之间交换grep序列的效果不佳,因为在我的情况下,它显示了很多 bleach
自己的依赖项。
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 bleach
s own dependencies.
这篇关于如何找到对python包的反向依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!