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: