-
Notifications
You must be signed in to change notification settings - Fork 425
[Fix] Replace deprecated pkg_resources with importlib
#1676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR migrates from the deprecated pkg_resources to the modern importlib.metadata API, which is the recommended approach for package introspection in Python 3.8+. The deprecated pkg_resources module is scheduled for complete removal in November 2025.
Key Changes:
- Replaced
pkg_resourcesimports withimportlib.metadatathroughout the codebase - Updated exception handling to use
PackageNotFoundErrorinstead ofDistributionNotFound - Implemented version-specific logic to handle differences between Python 3.8 and 3.9+ APIs
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| mmengine/utils/package_utils.py | Core migration from pkg_resources to importlib.metadata for all package introspection functions |
| tests/test_utils/test_package_utils.py | Updated test assertions to use PackageNotFoundError instead of deprecated exception types |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <[email protected]>
|
@HAOCHENYE This one is ready to be reviewed. |
mmengine/utils/package_utils.py
Outdated
| def install_package(package: str): | ||
| if not is_installed(package): | ||
| call_command(['python', '-m', 'pip', 'install', package]) | ||
| call_command(['python', '-m', 'pip', 'install', package]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the lint modification
2. Only keep `locate_file` branch. 3. keep the logic of `distribution package` and `import package` independent.
|
OK, a follow-up commit is pushed. |
|
You can follow https://github.com/open-mmlab/mmengine/blob/main/CONTRIBUTING.md to run lint locally |
|
I was misunderstanding your previous suggestion and disabled pre-commit 😆, restarting it soon. |
|
Lint is happy now.
This issue cause contributors being unable to run |
Great job! Honestly, mmengine's lint configuration is outdated. I am considering replacing yapf with black or ruff after merging #1665. |

This is a sub-PR of #1665
Brief
pkg_resourcesis no longer supported and is due to be completed abandoned in Nov. 2025. Replacing it withimportlib. This causes many modifications inmmengine/utils/package_utils.pyPyTest
The test unit is modified to
importlib:tests/test_utils/test_package_utils.py