Replace pkg_resources with importlib.metadata for Python 3.12 compati…#3328
Open
Robolightning wants to merge 1 commit into
Open
Replace pkg_resources with importlib.metadata for Python 3.12 compati…#3328Robolightning wants to merge 1 commit into
Robolightning wants to merge 1 commit into
Conversation
yuki-inaho
added a commit
to yuki-inaho/mmcv
that referenced
this pull request
May 29, 2026
Replace the pkg_resources import (removed in setuptools>=81) with importlib.metadata + packaging.version (open-mmlab#3326/open-mmlab#3328). The wheel build no longer needs the setuptools<81 workaround, so the GitHub Action and justfile build-wheel now install plain setuptools (+ packaging). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
This PR replaces the legacy
pkg_resourcesmodule with modernimportlib.metadatato ensure compatibility with Python 3.12.pkg_resourcesis 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_resourceswhenimportlib.metadatais not available.Modification
setup.pyto conditionally importimportlib.metadata(Python ≥ 3.8) or fall back topkg_resources(Python < 3.8).pkg_resources.get_distributionwith a wrapper that provides the same interface (get_distribution(name).version).pkg_resources.parse_versionwithpackaging.version.parse(with a fallback for environments wherepackagingis not installed).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’ssetup.pywill 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:
After PR: