Fix Python version in venv on Mac
I don't completely understand why, but __PYVENV_LAUNCHER__ is getting
set to /usr/local/bin/python3 which is confusing the CIPD Python into
creating virtualenvs that reference that system Python instead of the
CIPD Python. This fixes that.
Fixed: 59
Change-Id: Ie7a56797b3a25ced01a058afc7974c64c5fe04d1
diff --git a/pw_env_setup/py/pw_env_setup/virtualenv_setup/install.py b/pw_env_setup/py/pw_env_setup/virtualenv_setup/install.py
index b0f2a16..53b512c 100644
--- a/pw_env_setup/py/pw_env_setup/virtualenv_setup/install.py
+++ b/pw_env_setup/py/pw_env_setup/virtualenv_setup/install.py
@@ -96,8 +96,17 @@
pyvenv_cfg = os.path.join(venv_path, 'pyvenv.cfg')
if full_envsetup or not os.path.exists(pyvenv_cfg):
print('Creating venv at', venv_path)
+
+ # On Mac sometimes the CIPD Python has __PYVENV_LAUNCHER__ set to
+ # point to the system Python, which causes CIPD Python to create
+ # virtualenvs that reference the system Python instead of the CIPD
+ # Python. Clearing __PYVENV_LAUNCHER__ fixes that. See also pwbug/59.
+ envcopy = os.environ.copy()
+ if '__PYVENV_LAUNCHER__' in envcopy:
+ del envcopy['__PYVENV_LAUNCHER__']
+
cmd = (python, '-m', 'venv', '--clear', venv_path)
- subprocess.check_call(cmd)
+ subprocess.check_call(cmd, env=envcopy)
# The bin/ directory is called Scripts/ on Windows. Don't ask.
venv_bin = os.path.join(venv_path, 'Scripts' if os.name == 'nt' else 'bin')