Skip to content

Replace deprecated pkg_resources with importlib.metadata#1555

Open
ianhandy wants to merge 1636 commits intonvbn:masterfrom
ianhandy:fix/replace-pkg-resources
Open

Replace deprecated pkg_resources with importlib.metadata#1555
ianhandy wants to merge 1636 commits intonvbn:masterfrom
ianhandy:fix/replace-pkg-resources

Conversation

@ianhandy
Copy link

Summary

  • Replace all pkg_resources usage with importlib.metadata (stdlib since Python 3.8)
  • Falls back to importlib_metadata backport for older Python versions
  • Fixes setup.py pip version check and utils.py version lookup

Fixes #1552

Changes

setup.py: Replaced pkg_resources.get_distribution("pip").version with importlib.metadata.version("pip") and pkg_resources.DistributionNotFound with PackageNotFoundError.

thefuck/utils.py: Simplified get_installation_version() — removed pkg_resources fallback, using importlib.metadata with importlib_metadata backport fallback instead.

Test plan

  • All 1883 unit tests pass
  • Verified changes work on Python 3.9

🤖 Generated with Claude Code

nvbn and others added 30 commits October 10, 2017 08:31
* quick fix for nvbn#655

* Enabled by default and fix

* Test

* Added readme line

* This is unnecessary
Sample command output is:

    Error: This command updates brew itself, and does not take formula names.
    Use 'brew upgrade thefuck' instead.

This will never match the previous `"Use 'brew upgrade <formula>'" in command.output` test.
Fix nvbn#652

* Basic fix for nvbn#652
* Finishing work
* Added readme line
* Added test
* My test was stupid...
* Removed redundant lines
* That space...
Unfortunately, I didn't catch these issues while reviewing nvbn#702.

After looking more closely at `php` options, `-S` requires additional
arguments (<address>:<port>) and `-s` may produce output if used that
way. So, matching ` -s ` seems to be better.

Also, `@for_app('php')` already asserts the presence of `php ` in the
command script. Matching `php -s` prevents the rule from fixing commands
like `php -t public -s 0.0.0.0:8080`.
This helps you run `apt list --upgradable` after `apt update`,
as it suggests.
See nvbn#744 for context.

I'm personally okay with dropping Python 3.3 support,
but I'd like to at least get the tests working while we decide on that.
mrshu and others added 29 commits January 30, 2022 22:34
* Add missing comma in a test
* Add pikaur AUR manager to Arch Linux's commands

* Update README.md

* Update test_pacman_not_found.py

* Update pacman_not_found.py

* Update README.md
* macos shutdown sudo fix

* Update tests/rules/test_sudo.py

* Update thefuck/rules/sudo.py

* Update tests/rules/test_sudo.py

* Update thefuck/rules/sudo.py
* added updatedb(mlocate) to sudo rules

* Add test
* fix: brew_update_formula change output string

* fix: regex removed, test: backtick added

* Update tests/rules/test_brew_update_formula.py

* Update thefuck/rules/brew_update_formula.py
* fix: Update output for brew_install test: fixed

* chore: fixing flake8 styles

* feat: show more suggestions

* test: new functions added and multi suggestions

* refactor: rename to _get_suggestions
* Add git clone missing rule

* Clean up tests and improve matching

* Make rules behave correctly?

* Improve tests and redo matcher

* Remove unnecessary tests

* Improvements as per code review

* Remove dead tests

* Improve match function for git clone missing

* Improve tests

* Fix more tests

* Fix failing test

* Add Macos's /bin/sh command output to match

* Add output for lines uncovered by tests
* Add terraform 'no command' rule

* Feedback from PR
The imp module is deprecated and will be removed in Python 12.
* feat: Added nicer support for Windows CMD & Powershell

* Fix  typo

* Fix CMD console color after thefuck Ctrl-C

* Update setup.py


* Update setup.py


* Addressed PR comments

* fix spacing and newline issues

---------
pkg_resources (from setuptools) is deprecated and causes issues on
Python 3.12+ where it may not be available. Replace all usage with
importlib.metadata (stdlib since Python 3.8), with a fallback to the
importlib_metadata backport for older Python versions.

Fixes nvbn#1552
@ianhandy ianhandy force-pushed the fix/replace-pkg-resources branch from 1c4c4db to 92c8c67 Compare March 16, 2026 02:43
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