Skip to content

No easy way to get the distribution which provided a importlib.metadata.EntryPoint #86548

Closed

Description

@s0undt3ch
mannequin
BPO 42382
Nosy @jaraco, @s0undt3ch
PRs
  • bpo-42382: Make sure each EntryPoint carries it's Distribution information #23334
  • bpo-42382: In importlib.metadata, EntryPoint objects now expose dist #23758
  • Note: 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:

    assignee = 'https://github.com/jaraco'
    closed_at = <Date 2021-03-07.22:43:02.795>
    created_at = <Date 2020-11-17.06:56:21.443>
    labels = ['type-feature', 'library', '3.10']
    title = 'No easy way to get the distribution which provided a importlib.metadata.EntryPoint'
    updated_at = <Date 2021-03-07.22:47:18.618>
    user = 'https://github.com/s0undt3ch'

    bugs.python.org fields:

    activity = <Date 2021-03-07.22:47:18.618>
    actor = 'jaraco'
    assignee = 'jaraco'
    closed = True
    closed_date = <Date 2021-03-07.22:43:02.795>
    closer = 'jaraco'
    components = ['Library (Lib)']
    creation = <Date 2020-11-17.06:56:21.443>
    creator = 's0undt3ch'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 42382
    keywords = ['patch']
    message_count = 8.0
    messages = ['381211', '381235', '381239', '381243', '382605', '382610', '382611', '384126']
    nosy_count = 2.0
    nosy_names = ['jaraco', 's0undt3ch']
    pr_nums = ['23334', '23758']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue42382'
    versions = ['Python 3.10']

    Activity

    1. s0undt3ch commented on Nov 17, 2020

      s0undt3chmannequin
      MannequinAuthor

      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.

      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?

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-featureA feature request or enhancement
      on Nov 17, 2020
    3. jaraco commented on Nov 17, 2020

      @jaraco
      Member

      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.

    4. s0undt3ch commented on Nov 17, 2020

      s0undt3chmannequin
      MannequinAuthor

      Guess I jumped too fast :)

      Will the changes in CPythom be included in importlib_metadata?

    5. jaraco commented on Nov 17, 2020

      @jaraco
      Member

      Yes - I keep both in sync.

    6. jaraco commented on Dec 6, 2020

      @jaraco
      Member

      I've ported the initial patch over to the backport and am exploring options in python/importlib_metadata#266.

    7. jaraco commented on Dec 6, 2020

      @jaraco
      Member

      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?

    8. s0undt3ch commented on Dec 6, 2020

      s0undt3chmannequin
      MannequinAuthor

      Our software uses a plug-in based approach.
      Plugins are able to add/modify internal behavior, and, as part of bug submission process we have a CLI flag which provides information about the core app as well as any intervening plugins.

      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.

    9. jaraco commented on Dec 31, 2020

      @jaraco
      Member

      New changeset dfdca85 by Jason R. Coombs in branch 'master':
      bpo-42382: In importlib.metadata, EntryPoint objects now expose dist (bpo-23758)
      dfdca85

    10. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    3.10 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions