diff --git a/changelogs/fragments/ansible-test-venv-real-prefix.yaml b/changelogs/fragments/ansible-test-venv-real-prefix.yaml new file mode 100644 index 00000000000..c74fb047e99 --- /dev/null +++ b/changelogs/fragments/ansible-test-venv-real-prefix.yaml @@ -0,0 +1,2 @@ +bugfixes: + - ansible-test - Fix Python real prefix detection when running in a ``venv`` virtual environment. diff --git a/test/lib/ansible_test/_util/controller/tools/virtualenvcheck.py b/test/lib/ansible_test/_util/controller/tools/virtualenvcheck.py index 90dfa39410e..4c542a23fde 100644 --- a/test/lib/ansible_test/_util/controller/tools/virtualenvcheck.py +++ b/test/lib/ansible_test/_util/controller/tools/virtualenvcheck.py @@ -5,10 +5,22 @@ __metaclass__ = type import json try: + # virtualenv <20 from sys import real_prefix except ImportError: real_prefix = None +try: + # venv and virtualenv >= 20 + from sys import base_exec_prefix +except ImportError: + base_exec_prefix = None + +try: + from sys import base_exec_prefix +except ImportError: + base_exec_prefix = None + print(json.dumps(dict( - real_prefix=real_prefix, + real_prefix=real_prefix or base_exec_prefix, )))