Skip to content

Replace pkg_resources with importlib.metadata for Python 3.12 compati…#3328

Open
Robolightning wants to merge 1 commit intoopen-mmlab:mainfrom
Robolightning:main
Open

Replace pkg_resources with importlib.metadata for Python 3.12 compati…#3328
Robolightning wants to merge 1 commit intoopen-mmlab:mainfrom
Robolightning:main

Conversation

@Robolightning
Copy link
Copy Markdown

Motivation

This PR replaces the legacy pkg_resources module with modern importlib.metadata to ensure compatibility with Python 3.12.
pkg_resources is deprecated and no longer installed by default in Python 3.12+, causing installation failures when building from source (see issue #3325).
The change maintains full backward compatibility with Python < 3.8 by falling back to pkg_resources when importlib.metadata is not available.

Modification

  • Modified setup.py to conditionally import importlib.metadata (Python ≥ 3.8) or fall back to pkg_resources (Python < 3.8).
  • Replaced direct usage of pkg_resources.get_distribution with a wrapper that provides the same interface (get_distribution(name).version).
  • Replaced pkg_resources.parse_version with packaging.version.parse (with a fallback for environments where packaging is not installed).
  • Added a compatibility layer for DistributionNotFound / PackageNotFoundError.

These changes are isolated to the installation script and do not affect runtime behaviour.

BC-breaking (Optional)

No breaking changes.
The new implementation is fully compatible with older Python versions (3.6+) thanks to the fallback mechanism.
Downstream projects relying on mmcv’s setup.py will not observe any difference.

Use cases (Optional)

Not applicable – this is a maintenance change to enable successful installation under Python 3.12.

Checklist

Before PR:

  • I have read and followed the workflow indicated in the CONTRIBUTING.md to create this PR.
  • Pre-commit or linting tools indicated in CONTRIBUTING.md are used to fix the potential lint issues.
  • Bug fixes are covered by unit tests, the case that causes the bug should be added in the unit tests.
  • New functionalities are covered by complete unit tests. If not, please add more unit test to ensure the correctness.
  • The documentation has been modified accordingly, including docstring or example tutorials.

After PR:

  • If the modification has potential influence on downstream or other related projects, this PR should be tested with some of those projects, like MMDet or MMCls.
  • CLA has been signed and all committers have signed the CLA in this PR.

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Feb 23, 2026

CLA assistant check
All committers have signed the CLA.

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