Fix: replace pkg_resources with importlib.metadata for setuptools 82+ by junagent · Pull Request #1558 · nvbn/thefuck · GitHub
Skip to content

Fix: replace pkg_resources with importlib.metadata for setuptools 82+#1558

Open
junagent wants to merge 1 commit intonvbn:masterfrom
junagent:junagent/fix/replace-pkg-resources-importlib-metadata
Open

Fix: replace pkg_resources with importlib.metadata for setuptools 82+#1558
junagent wants to merge 1 commit intonvbn:masterfrom
junagent:junagent/fix/replace-pkg-resources-importlib-metadata

Conversation

@junagent
Copy link
Copy Markdown

Summary

pkg_resources was removed from setuptools 82.0.0 (PEP 740). This causes python setup.py to fail with:

ModuleNotFoundError: No module named 'pkg_resources'

This PR replaces pkg_resources.get_distribution("pip").version with importlib.metadata.version(), which is the stdlib equivalent available since Python 3.8.

Changes

setup.py — replace pkg_resources with importlib.metadata:

# Before
import pkg_resources
...
try:
    if int(pkg_resources.get_distribution("pip").version.split('.')[0]) < 6:
        ...
except pkg_resources.DistributionNotFound:
    pass

# After
from importlib.metadata import version as get_version, PackageNotFoundError
...
try:
    if int(get_version("pip").split('.')[0]) < 6:
        ...
except PackageNotFoundError:
    pass

Verification

  • python setup.py egg_info — works without pkg_resources
  • python -m build (sdist) — works without pkg_resources
  • pip version check (>=6) still enforced
  • Falls back gracefully if pip not found

Related

Fixes #1552

Fixes nvbn#1552

pkg_resources was removed from setuptools 82.0.0 (PEP 740). This caused
'python setup.py' to fail with:

    ModuleNotFoundError: No module named 'pkg_resources'

Replace pkg_resources.get_distribution("pip").version with
importlib.metadata.version(), which is the stdlib equivalent available
since Python 3.8.

Verification:
- setup.py runs without pkg_resources
- pip version check still works (>=6 required)
- Falls back gracefully if pip not installed
@junagent
Copy link
Copy Markdown
Author

Comment thread setup.py
Comment on lines +11 to +13
print('pip older than 6.0 not supported, please upgrade pip with:

'
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not related to the replacement of pkg_resources with importlib.metadata.

Comment thread setup.py

if sys.platform == "win32":
scripts = ['scripts\\fuck.bat', 'scripts\\fuck.ps1']
scripts = ['scripts\fuck.bat', 'scripts\fuck.ps1']
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this change related to the replacement of pkg_resources with importlib.metadata.

Comment thread setup.py
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setup.py lists python 2.7+ as supported however importlib.metadata is only available from python 3.8+. Should importlib-metadata not be used covering python 2.7 to 3.7 and should also be added to setup.py?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use of deprecated / removed pkg_resources

2 participants