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
/usr/bin/rst2html.py
) but my question is how to do it in general.