Open MicaelJarniac opened 6 months ago
Thanks for opening this issue.
Do you know how exactly VSCode activates venv?
What do I need to do to activate venv myself through VSCode?
Thanks for opening this issue.
Do you know how exactly VSCode activates venv?
What do I need to do to activate venv myself through VSCode?
To have VS Code activate venvs for you, at least in Python, I believe you need Python installed on your system, and the Python extension on VS Code. Here's their readme showing how to select an interpreter:
https://github.com/Microsoft/vscode-python?tab=readme-ov-file#set-up-your-environment
You open a .py
file, and then on the bottom of the screen there'll be a button that allows you to choose which venv you want to use.
Pressing it should display a modal like this:
If you create a venv on the current folder, for example with python -m venv .venv
, it should be available on the list for you to choose from.
Once a venv is selected, new terminals opened inside VS Code (with Ctrl+` for example) should have it activated automatically.
As for the exact mechanism that activates it, I'm not entirely sure, but I believe it's handled by the VS Code shell integration injecting and modifying env vars, like prepending the .venv/bin
folder to PATH
, and setting VIRTUAL_ENV
to .venv
.
I believe the issue with this new activation method is this, but I can't find the exact code changes:
Impressively fast response time by the way!
This issue is a bit strange for me, though, as it seems to break things only partially. Like, sometimes the PATH
will be modified, but the items in it will be in the wrong order.
With integrated tmux:
❯ which python
/home/micael/.pyenv/shims/python
❯ echo $PATH
/home/micael/.pyenv/shims:/home/micael/bin:/home/micael/.vscode/extensions/ms-python.python-2024.7.11341012/python_files/deactivate/zsh:/home/micael/projects/playground/.venv/bin:/home/micael/.pyenv/bin:/home/micael/.local/bin:/home/micael/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/micael/.cache/zsh4humans/v5/fzf/bin
❯ echo $VIRTUAL_ENV
/home/micael/projects/playground/.venv
Without integrated tmux:
❯ which python
/home/micael/projects/playground/.venv/bin/python
❯ echo $PATH
/home/micael/.vscode/extensions/ms-python.python-2024.7.11341012/python_files/deactivate/zsh:/home/micael/projects/playground/.venv/bin:/home/micael/.pyenv/shims:/home/micael/bin:/home/micael/.vscode/extensions/ms-python.python-2024.7.11341012/python_files/deactivate/zsh:/home/micael/projects/playground/.venv/bin:/home/micael/.pyenv/bin:/home/micael/.local/bin:/home/micael/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/micael/.cache/zsh4humans/v5/fzf/bin
❯ echo $VIRTUAL_ENV
/home/micael/projects/playground/.venv
And it seems to behave differently now, after a couple of days without using integrated tmux. Not sure why.
I'll also try investigating whether this could be due to how Pyenv is configured, as it appears to be prepending itself to PATH
after VS Code prepends the venv.
Here are my Zsh config files:
I cannot offer to debug the problem but if you figure out what VSCode does, and describe what exactly I need to click/type in VSCode to activate venv, there is a chance I'll do something.
Seems like VIRTUAL_ENV
does indeed break with integrated tmux:
❯ echo $PATH
/home/micael/.pyenv/shims:/home/micael/bin:/home/micael/.vscode/extensions/ms-python.python-2024.7.11341012/python_files/deactivate/zsh:/home/micael/projects/foo/bar/.venv/bin:/home/micael/.pyenv/bin:/home/micael/.local/bin:/home/micael/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/micael/.cache/zsh4humans/v5/fzf/bin
❯ echo $VIRTUAL_ENV
/home/micael/projects/playground/.venv
Here, I have project foo/bar
active, but VIRTUAL_ENV
still points to playground
.
I cannot offer to debug the problem but if you figure out what VSCode does, and describe what exactly I need to click/type in VSCode to activate venv, there is a chance I'll do something.
Thanks a lot for this project and for taking the time to reply here. I'll investigate this further, and report back if I have anything worth sharing.
Shot in the dark: try adding VIRTUAL_ENV
to $Z4H/zsh4humans/.tmux.conf
.
A few months ago, VS Code updated the way they activate virtual environments (at least for Python, I don't know about other languages).
The way it worked before was, after opening the terminal inside VS Code, it'd quickly "type" the venv activation command (something like
source /foo/bar/baz/.venv/bin/activate
), then run it, and also clear the screen I think.This old method worked fine with Zsh for Humans and integrated tmux (after fixing #258 and #260).
Now, however, VS Code seems to no longer rely on automatically typing commands, and instead uses other methods, like injecting env vars, I think.
This new method breaks with Z4H and integrated tmux. Disabling tmux makes it work, though, at the expense of losing some Z4H features like the prompt at the bottom, which I really liked.
Here are some related issues and links:
https://github.com/microsoft/vscode-python/issues/22879
https://github.com/microsoft/vscode-python/issues/22289
https://github.com/microsoft/vscode/issues/205133
https://github.com/microsoft/vscode-python/issues/23113
https://github.com/microsoft/vscode-python/wiki/Activate-Environments-in-Terminal-Using-Environment-Variables
https://github.com/microsoft/vscode-python/issues/11039
I understand that this project has limited support and is provided as-is. I'm opening this issue mostly to inform others about this problem and a possible workaround.
And this may even be a problem with VS Code, not with this project.