Skip to content

Conversation

@MGAMZ
Copy link
Contributor

@MGAMZ MGAMZ commented Oct 26, 2025

This is a sub-PR of #1665

Brief

pkg_resources is no longer supported and is due to be completed abandoned in Nov. 2025. Replacing it with importlib. This causes many modifications in mmengine/utils/package_utils.py

PyTest

The test unit is modified to importlib: tests/test_utils/test_package_utils.py

=================================================== test session starts ===================================================
platform linux -- Python 3.13.9, pytest-8.4.1, pluggy-1.6.0 -- /home/mgam/miniforge3/envs/pt/bin/python3.13
cachedir: .pytest_cache
rootdir: /home/mgam/mgam_repos/mmengine
configfile: pytest.ini
plugins: anyio-4.9.0, hydra-core-1.3.2
collected 2 items                                                                                                         

tests/test_utils/test_package_utils.py::test_is_installed PASSED                                                    [ 50%]
tests/test_utils/test_package_utils.py::test_get_install_path PASSED                                                [100%]

==================================================== 2 passed in 0.19s ====================================================

Copilot AI review requested due to automatic review settings October 26, 2025 08:52
Copy link
Contributor

Copilot AI left a 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_resources imports with importlib.metadata throughout the codebase
  • Updated exception handling to use PackageNotFoundError instead of DistributionNotFound
  • 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.

@MGAMZ
Copy link
Contributor Author

MGAMZ commented Oct 26, 2025

@HAOCHENYE This one is ready to be reviewed.

def install_package(package: str):
if not is_installed(package):
call_command(['python', '-m', 'pip', 'install', package])
call_command(['python', '-m', 'pip', 'install', package])
Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove the lint modification

MGAMZ added 2 commits October 26, 2025 20:26
2. Only keep `locate_file` branch.
3. keep the logic of `distribution package` and `import package` independent.
@MGAMZ
Copy link
Contributor Author

MGAMZ commented Oct 26, 2025

OK, a follow-up commit is pushed.

@HAOCHENYE
Copy link
Collaborator

Besides,fix the lint.
image

You can follow https://github.com/open-mmlab/mmengine/blob/main/CONTRIBUTING.md to run lint locally

@MGAMZ
Copy link
Contributor Author

MGAMZ commented Oct 27, 2025

I was misunderstanding your previous suggestion and disabled pre-commit 😆, restarting it soon.

@MGAMZ
Copy link
Contributor Author

MGAMZ commented Oct 27, 2025

Lint is happy now.

yapf=0.32.0 requires lib2to3 which is deprecated since python=3.11, and is removed in python=3.13. 2to3 Doc

This issue cause contributors being unable to run yapf lint in python>=3.13. So I suggest upgrade the yapf version in pre-commit which no longer requires this deprecated lib.

@HAOCHENYE
Copy link
Collaborator

Lint is happy now.

yapf=0.32.0 requires lib2to3 which is deprecated since python=3.11, and is removed in python=3.13. 2to3 Doc

This issue cause contributors being unable to run yapf lint in python>=3.13. So I suggest upgrade the yapf version in pre-commit which no longer requires this deprecated lib.

Great job! Honestly, mmengine's lint configuration is outdated. I am considering replacing yapf with black or ruff after merging #1665.

@HAOCHENYE HAOCHENYE merged commit 2543429 into open-mmlab:main Oct 27, 2025
6 of 15 checks passed
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.

2 participants