Replace pkg_resources with modern packaging alternatives in devops tasks and smoketest #41973
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.
Summary
This PR removes the remaining usage of the deprecated
pkg_resources
module in two files and replaces it with modern packaging alternatives (importlib.metadata
andpackaging
library).Problem
The
pkg_resources
module is deprecated and should be replaced with standard library and packaging library alternatives. Two files were still usingpkg_resources
:scripts/devops_tasks/common_tasks.py
- Usedpkg_resources.WorkingSet
andworking_set
for package discoverycommon/smoketest/dependencies.py
- Usedpkg_resources.working_set.by_key
for dependency resolutionChanges Made
scripts/devops_tasks/common_tasks.py
from pkg_resources import parse_version, parse_requirements, Requirement, WorkingSet, working_set
importlib.metadata
,packaging.version.parse
,packaging.requirements.Requirement
get_installed_packages()
to useimportlib.metadata.distributions()
with proper path filteringcommon/smoketest/dependencies.py
import pkg_resources
withimportlib.metadata
andpackaging.requirements
get_dependencies()
to useimportlib.metadata.distribution()
combine_requirements()
to work withpackaging.requirements.Requirement
objects:project_name
→name
specs
→specifier
Compatibility
importlib.metadata
with fallback toimportlib_metadata
for older versionspkg_resources
implementationTesting
Extensive testing was performed including:
test_regression.py
Example Usage
The functions work exactly as before:
Benefits
importlib.metadata
is part of the Python standard library (3.8+)Fixes the deprecation warnings and prepares the codebase for future Python versions where
pkg_resources
may be removed entirely.This pull request was created as a result of the following prompt from Copilot chat.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.