[Fix] Replace deprecated pkg_resources with importlib#1676
[Fix] Replace deprecated pkg_resources with importlib#1676HAOCHENYE merged 7 commits intoopen-mmlab:mainfrom
pkg_resources with importlib#1676Conversation
There was a problem hiding this comment.
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 <175728472+Copilot@users.noreply.github.com>
|
@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]) No newline at end of file |
There was a problem hiding this comment.
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