-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
No easy way to get the distribution which provided a importlib.metadata.EntryPoint #86548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
With USE_IMPORTLIB_METADATA_STDLIB = USE_IMPORTLIB_METADATA = False
try:
# Py3.8+
import importlib.metadata
USE_IMPORTLIB_METADATA_STDLIB = True
except ImportError:
# < Py3.8 backport package
import importlib_metadata
USE_IMPORTLIB_METADATA = True
def get_distribution_from_entry_point(entry_point):
loaded_entry_point = entry_point.load()
if isinstance(loaded_entry_point, types.ModuleType):
module_path = loaded_entry_point.__file__
else:
module_path = sys.modules[loaded_entry_point.__module__].__file__
if USE_IMPORTLIB_METADATA_STDLIB:
distributions = importlib.metadata.distributions
else:
distributions = importlib_metadata.distributions
for distribution in distributions():
try:
relative = pathlib.Path(module_path).relative_to(
distribution.locate_file("")
)
except ValueError:
pass
else:
if relative in distribution.files:
return distribution The above solution has the additional drawback that you're iterating the distributions list, once per EntryPoint, which, was already iterated to get the entry-points listing. I propose we attach the Distribution instance to each of the found EntryPoint to avoid this low performance workaround. I don't have an issue with providing a pull-request, but should that pull request be done against |
Pedro - thanks for the detailed report. Pull requests against importlib_metadata are easier to accept because they can be tested more easily, released more rapidly, and there's a straightforward way to port them to CPython. Regardless, I see you've proposed a change to CPython, so I can work with that. |
Guess I jumped too fast :) Will the changes in CPythom be included in |
Yes - I keep both in sync. |
I've ported the initial patch over to the backport and am exploring options in python/importlib_metadata#266. |
In discussion, I realized that I don't yet understand what use-cases drive this demand? What code is it that requires resolving a distribution from an entry point? |
Our software uses a plug-in based approach. This is where we need to "map" an entry point to it's distribution, so we know the name and version of it to display on this report. |
EntryPoint
carries it'sDistribution
information #23334EntryPoint
objects now exposedist
#23758Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: