question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Setuptools overriding standard lib `importlib.metadata.PathDistribution`

See original GitHub issue

setuptools version

60.9.3

Python version

python 3.9

OS

macOS

Additional environment information

❯ pip list
Package    Version
---------- -------
pip        22.0.4
setuptools 60.9.3
wheel      0.37.1

Description

this seems similar to https://github.com/pypa/setuptools/issues/3102, but issue was closed by #3108, without activity since then, and I still seeing this issue in the latest release.

importing setuptools overrides/removes the behavior and return type of of the standard library importlib.metadata.distribution() function:

Expected behavior

importing setuptools doesn’t supersede/remove the distribution finder from standard library importlib

How to Reproduce

  1. import setuptools
  2. call importlib.metadata.distribution()

Output

>>> from importlib import metadata
>>> metadata.distribution('pip')  # returns the expected type
<importlib.metadata.PathDistribution object at 0x101665910>
>>> import sys
>>> [getattr(finder, 'find_distributions', None) for finder in sys.meta_path]
[None, None, None, <bound method PathFinder.find_distributions of <class '_frozen_importlib_external.PathFinder'>>]

# importing setuptools changes the type of object returned by metadata.distribution
# and removes the _frozen_importlib_external.PathFinder from 
# the list of finders used by `metadata.Distribution.discover`
>>> import setuptools
>>> metadata.distribution('pip')
<setuptools._vendor.importlib_metadata.PathDistribution object at 0x102253070>
>>> [getattr(finder, 'find_distributions', None) for finder in sys.meta_path]
[None, None, None, None, None, None, <bound method MetadataPathFinder.find_distributions of <setuptools._vendor.importlib_metadata.MetadataPathFinder object at 0x101fdad00>>]

# note:
>>> import importlib_metadata
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'importlib_metadata'

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
abravalhericommented, Mar 13, 2022

I think it would be nice to avoid breaking the users’ expectations like it is happening right now, but since this workaround comes directly from importlib_metadata, there might be very little we can do in setuptools 😓

This workaround also seems necessary for importlib_metadata to work properly.

1reaction
tlambert03commented, Mar 13, 2022

I think the best here is to do a structural check, since setuptools is not the only dependency that can break your plugin architecture.

I agree. thanks for having a look. If you feel like this is just how it’s going to be, I can close this issue (but feel free to re-open if you think there’s anything actionable here on your end)

Read more comments on GitHub >

github_iconTop Results From Across the Web

ImportError: cannot import name 'metadata' from 'importlib'
Today, I ran into an error similar (but not exactly the same) to yours. Starting from Python 3.8, the importlib module has a...
Read more >
Using importlib.metadata — Python 3.11.1 documentation
importlib_metadata is a library that provides access to the metadata of an installed Distribution Package, such as its entry points or its top-level...
Read more >
History - setuptools 65.6.3.post20221216 documentation
Future releases of setuptools may simply ignore externally set metadata not backed by dynamic or even halt the build with an error.
Read more >
Changelog — pytest documentation
#9741: On Python 3.11, use the standard library's tomllib to parse TOML. ... issue #1953: Fixed error when overwriting a parametrized fixture, ...
Read more >
Python - Using importlib.metadata | Docs4dev
importlib.metadata is a library that provides for access to installed package metadata. Built in part on Python's import system, this library ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found