I have a file that I suspect was installed by 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
. Does it exist? Or should I use some combination of pip
and grep
?
In that case, I don't know how to list all the file installed.
To do so, we can use the pip list -o or pip list --outdated command, which returns a list of packages with the version currently installed and the latest available. On the other hand, to list out all the packages that are up to date, we can use the pip list -u or pip list --uptodate command.
The pip command looks for the package in PyPI, resolves its dependencies, and installs everything in your current Python environment to ensure that requests will work. The pip install <package> command always looks for the latest version of the package and installs it.
There are three ways to get the list of all the libraries or packages or modules installed in python using pip list command, pip freeze command and help function . This will list all the modules installed in the system .
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:
#!/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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With