[英]How to find which pip package owns a file?
I have a file that I suspect was installed by pip
. 我有一个我怀疑是由pip
安装的文件。 How can I find which package installed that file? 如何找到安装该文件的软件包?
In other words, I'm looking for a command similar to pacman -Qo filename
or dpkg -S filename
, but for pip
. 换句话说,我正在寻找类似于pacman -Qo filename
或dpkg -S filename
,但是对于pip
。 Does it exist? 它存在吗? Or should I use some combination of pip
and grep
? 或者我应该使用pip
和grep
某种组合? In that case, I don't know how to list all the file installed. 在这种情况下,我不知道如何列出安装的所有文件。
You could try with 你可以试试
pip list | tail -n +3 | cut -d" " -f1 | xargs pip show -f | grep "filename"
Then search through the results looking for that file. 然后搜索查找该文件的结果。
You can use a python script like this: 您可以使用这样的python脚本:
#!/usr/bin/env python
import sys
try:
from pip.utils import get_installed_distributions
except ModuleNotFoundError:
from pip._internal.utils.misc import get_installed_distributions
MYPATH=sys.argv[1]
for dist in get_installed_distributions():
# RECORDs should be part of .dist-info metadatas
if dist.has_metadata('RECORD'):
lines = dist.get_metadata_lines('RECORD')
paths = [l.split(',')[0] for l in lines]
# Otherwise use pip's log for .egg-info's
elif dist.has_metadata('installed-files.txt'):
paths = dist.get_metadata_lines('installed-files.txt')
else:
paths = []
if MYPATH in paths:
print(dist.project_name)
Usage looks like this: 用法如下:
$ python lookup_file.py requests/__init__.py
requests
I wrote a more complete version here, with absolute paths: 我在这里用绝对路径写了一个更完整的版本:
https://github.com/nbeaver/pip_file_lookup https://github.com/nbeaver/pip_file_lookup
Try this! 试试这个!
find_pkg_by_filename(){ for pkg in $(pip list | cut -d" " -f1) ; do if pip show -f "$pkg" | grep "$1" ; then echo "=== Above files found in package $pkg ===" ; fi ; done ; }
find_pkg_by_filename somefilename
Note that if you add -q
to the grep
, it will exit as soon as there's a match, and then pip will complain about broken pipes. 请注意,如果将-q
添加到grep
,它会在匹配时立即退出,然后pip会抱怨管道损坏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.