tl;dr how do I discover which package installed a script entry point under Scripts
directory?
Given a Python environment at path Python
, it has a scripts entry points directory at Python/Scripts
. For script Python/Scripts/foo.py
, how do I discover which Python package installed foo.py
?
Or in other words, how do I "reverse lookup" Python script entry points?
You can list all files belonging to a given package with importlib.metadata.files(<package_name>)
. From there, writing a script in order to find what package a file belongs to is quite straigthforward:
import importlib.metadata
def find_file_pkg(file_path):
for p in importlib.metadata.packages_distributions():
try:
for f in importlib.metadata.files(p):
if file_path in str(f):
return p
except importlib.metadata.PackageNotFoundError:
pass
return None
if __name__ == '__main__':
print(find_file_pkg('Scripts/foo.py'))
Edit/Note: it is usually recommended to set executables as EntryPoints. I first oversaw that your entry point is a .py
file and proposed the below answer:
I'm not sure pip
allows that but you can do it with python importlib.metadata
.
First find out which module/function is launched by foo.exe
with:
import importlib.metadata
importlib.metadata.entry_points().select(name='foo')
It will output something like:
[EntryPoint(name='foo', value='foo_module:foo_function', group='console_scripts')]
Then you get the package distribution with:
importlib.metadata.packages_distributions()['foo_module']