New submission from Pedro Algarvio <pe...@algarvio.me>:

With `pkg_resources` an `EntryPoint` has a dist attribute which allows you to 
get the distribution that provided that specific entry-point, however, with 
`importlib.metafata` and `importlib_metadata` that's not an east task.

```python
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 `importlib_metadata` or `importlib.metadata`?

----------
components: Library (Lib)
messages: 381211
nosy: jaraco, s0undt3ch
priority: normal
severity: normal
status: open
title: No easy way to get the distribution which provided a 
importlib.metadata.EntryPoint
type: enhancement

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue42382>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to