From 4b4479844b5236bedd6fa0f755c213814eaa7b13 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Wed, 11 Feb 2026 07:27:45 +0100 Subject: [PATCH] [PR #11497/d9e48f04 backport][stable-10] [stable-11] python_requirements_info: use importlib.metadata when available (#11495) (#11498) [stable-11] python_requirements_info: use importlib.metadata when available (#11495) (#11497) python_requirements_info: use importlib.metadata when available (#11495) Use importlib.metadata when available. (cherry picked from commit 88adca3fb4b88d4470debf5479ee55aea62803e3) (cherry picked from commit d9e48f0488144e0ecb1e0c4171373963f2833e65) Co-authored-by: Felix Fontein --- .../fragments/11492-python_requires_info.yml | 4 ++ plugins/modules/python_requirements_info.py | 44 +++++++++++++------ 2 files changed, 35 insertions(+), 13 deletions(-) create mode 100644 changelogs/fragments/11492-python_requires_info.yml diff --git a/changelogs/fragments/11492-python_requires_info.yml b/changelogs/fragments/11492-python_requires_info.yml new file mode 100644 index 0000000000..aa52ffca43 --- /dev/null +++ b/changelogs/fragments/11492-python_requires_info.yml @@ -0,0 +1,4 @@ +bugfixes: + - "python_requirements_info - use ``importlib.metadata`` if ``pkg_resources`` from ``setuptools`` cannot be imported. + That module has been removed from setuptools 82.0.0 + (https://github.com/ansible-collections/community.general/issues/11491, https://github.com/ansible-collections/community.general/pull/11492)." diff --git a/plugins/modules/python_requirements_info.py b/plugins/modules/python_requirements_info.py index cbe93dd944..977c444e67 100644 --- a/plugins/modules/python_requirements_info.py +++ b/plugins/modules/python_requirements_info.py @@ -123,15 +123,25 @@ import re import sys import operator -HAS_DISTUTILS = False +from ansible.module_utils.basic import AnsibleModule + +from ansible_collections.community.general.plugins.module_utils.version import LooseVersion + +HAS_IMPORTLIB_METADATA = False try: - import pkg_resources - from ansible_collections.community.general.plugins.module_utils.version import LooseVersion - HAS_DISTUTILS = True + import importlib.metadata + + HAS_IMPORTLIB_METADATA = True except ImportError: pass -from ansible.module_utils.basic import AnsibleModule +HAS_DISTUTILS = False +try: + import pkg_resources + + HAS_DISTUTILS = True +except ImportError: + pass operations = { '<=': operator.le, @@ -157,9 +167,9 @@ def main(): ), supports_check_mode=True, ) - if not HAS_DISTUTILS: + if not HAS_DISTUTILS and not HAS_IMPORTLIB_METADATA: module.fail_json( - msg='Could not import "distutils" and "pkg_resources" libraries to introspect python environment.', + msg='Could not import "pkg_resources" or "importlib.metadata" libraries to introspect Python environment.', python=sys.executable, python_version=sys.version, python_version_info=python_version_info, @@ -180,12 +190,20 @@ def main(): pkg, op, version = match.groups() if op is not None and op not in operations: module.fail_json(msg="Failed to parse version requirement '{0}'. Operator must be one of >, <, <=, >=, or ==".format(dep)) - try: - existing = pkg_resources.get_distribution(pkg).version - except pkg_resources.DistributionNotFound: - # not there - results['not_found'].append(pkg) - continue + if HAS_DISTUTILS: + try: + existing = pkg_resources.get_distribution(pkg).version + except pkg_resources.DistributionNotFound: + # not there + results['not_found'].append(pkg) + continue + else: + try: + existing = importlib.metadata.version(pkg) + except importlib.metadata.PackageNotFoundError: + # not there + results['not_found'].append(pkg) + continue if op is None and version is None: results['valid'][pkg] = { 'installed': existing,