From 482ce8fa8bf100ccad50f10fd8116fd732ae99db Mon Sep 17 00:00:00 2001 From: Matt Davis <6775756+nitzmahone@users.noreply.github.com> Date: Thu, 9 Jun 2022 08:11:00 -0700 Subject: [PATCH] fix pip module resolution (#78000) (#78005) * `importlib.util` appears to be lazily imported and is sometimes unavailable as an attribute of `importlib` without an explicit import (cherry picked from commit 6e78425f8d6edbfd95faf5c3c2c05c6d3f038758) --- changelogs/fragments/pip-lazy-import.yml | 2 ++ lib/ansible/modules/pip.py | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/pip-lazy-import.yml diff --git a/changelogs/fragments/pip-lazy-import.yml b/changelogs/fragments/pip-lazy-import.yml new file mode 100644 index 00000000000..dd5d6b5b8ad --- /dev/null +++ b/changelogs/fragments/pip-lazy-import.yml @@ -0,0 +1,2 @@ +bugfixes: +- pip - fix cases where resolution of pip Python module fails when importlib.util has not already been imported diff --git a/lib/ansible/modules/pip.py b/lib/ansible/modules/pip.py index f2d9cc1e646..2a53efd8fd2 100644 --- a/lib/ansible/modules/pip.py +++ b/lib/ansible/modules/pip.py @@ -455,15 +455,15 @@ def _get_pip(module, env=None, executable=None): def _have_pip_module(): # type: () -> bool """Return True if the `pip` module can be found using the current Python interpreter, otherwise return False.""" try: - import importlib + from importlib.util import find_spec except ImportError: - importlib = None + find_spec = None # type: ignore[assignment] # type: ignore[no-redef] - if importlib: + if find_spec: # noinspection PyBroadException try: # noinspection PyUnresolvedReferences - found = bool(importlib.util.find_spec('pip')) + found = bool(find_spec('pip')) except Exception: found = False else: