Skip to content

pythonTerminalEnvVarActivation shadows pyenv PATHΒ #23227

@kbehlers

Description

@kbehlers

Behaviour

When the pythonTerminalEnvVarActivation experiment is active, I can no longer use pyenv-virtualenv commands for activating and deactivating venvs in the VSCode terminal. Instead the interpreter selected in the Python extension always stays activated in the terminal.

The pyenv-virtualenv commands give the appearance of working (they change my zsh command prompt showing the active venv) but running python commands like python --version or pip freeze show that the python interpreter being used isn't the one shown in my command prompt but instead the one selected using the Python extension in VSCode.

It seems possibly related to how pythonTerminalEnvVarActivation prepends to PATH.

Steps to reproduce:

  1. Make sure settings.json has
"python.experiments.optInto": ["pythonTerminalEnvVarActivation"]
  1. Install pyenv and pyenv-virtualenv
  2. Install two versions of python with pyenv (e.g. pyenv install 3.10 pyenv install 3.9) to make it easier to see when the wrong venv is active
  3. Create a virtualenv for each version of python
pyenv virtualenv 3.9 first_example_env
pyenv virtualenv 3.10 second_example_env
  1. Choose first_example_env as the python interpreter in VSCode using Python: Select Interpreter
  2. Open a VSCode terminal
  3. echo $PATH (this shows vscode specific paths when the experiment is enabled)
  4. python --version (should say Python 3.9.*)
  5. pyenv deactivate (doesn't end up deactivating as it should but gives the appearance of doing so by changing the command prompt)
  6. pyenv activate second_example_env (changes the command prompt, doesn't actually change the venv though)
  7. python --version (should say Python 3.10.* but still says Python 3.9.* because the environment didn't really switch)
  8. Use Python: Select Interpreter and choose second_example_env
  9. Reload the VSCode terminal
  10. Repeat above steps, except this time second_example_env will be active no matter what

If I instead add:

"python.experiments.optOutFrom": ["pythonTerminalEnvVarActivation"]

to settings.json, I can activate and deactivate venvs using pyenv-virtualenv just as I would expect, where the venv actually switches in the terminal.

Diagnostic data

Output for Python in the Output panel (View→Output, change the drop-down the upper-right of the Output panel to Python)

2024-04-11 11:24:39.228 [info] Prepending environment variable PATH in collection with /Users/username/.vscode/extensions/ms-python.python-2024.4.1/python_files/deactivate/zsh:/Users/username/.pyenv/versions/3.9.17/envs/first_example_env/bin: {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2024-04-11 11:24:39.228 [info] Setting environment variable VIRTUAL_ENV in collection to /Users/username/.pyenv/versions/3.9.17/envs/first_example_env {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2024-04-11 11:24:39.228 [info] Prepending environment variable PS1 in collection with (first_example_env)  {"applyAtShellIntegration":true,"applyAtProcessCreation":false}
2024-04-11 11:24:39.228 [error] Failed to initialize deactivate script /bin/zsh [Error: "/Users/username/.vscode/extensions/ms-python.python-2024.4.1/python_files/deactivate/zsh/envVars.txt" file not created
	at Timeout.<anonymous> (/Users/username/.vscode/extensions/ms-python.python-2024.4.1/out/client/extension.js:2:271175)
	at listOnTimeout (node:internal/timers:569:17)
	at process.processTimers (node:internal/timers:512:7)]

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions