summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Davis <6775756+nitzmahone@users.noreply.github.com>2022-06-09 08:11:00 -0700
committerGitHub <noreply@github.com>2022-06-09 10:11:00 -0500
commit482ce8fa8bf100ccad50f10fd8116fd732ae99db (patch)
tree7d7748e9aa596e7bc6fcc6dd0edcdfd4fb4aefa2
parent2cc9333e9733f43a6269c9fa58ea2e18246a12aa (diff)
downloadansible-482ce8fa8bf100ccad50f10fd8116fd732ae99db.tar.gz
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)
-rw-r--r--changelogs/fragments/pip-lazy-import.yml2
-rw-r--r--lib/ansible/modules/pip.py8
2 files changed, 6 insertions, 4 deletions
diff --git a/changelogs/fragments/pip-lazy-import.yml b/changelogs/fragments/pip-lazy-import.yml
new file mode 100644
index 0000000000..dd5d6b5b8a
--- /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 f2d9cc1e64..2a53efd8fd 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: