I'm having an issue with configuring tasks for VSCode while using pyenv-virtualenv (not virtual env!), kind of similar to this but it is not solved and it also refers only to .zshrc not .zprofile.
I have a a pyenv environment configured and that python interpreter is selected in the project. In terminals inside VSCode I can execute code and the terminal automatically has loaded the correct pyenv.
However, when running VSCode tasks the task fails to activate the pyenv virtualenv of the project.
E.g. in the terminal of my project I get:
(proj) % pyenv local
projenv
(proj) % python --version
Python 3.10.4
I've tried .vscode/tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "Run some python",
"type": "shell",
"command": "pyenv activate projenv && python --version",´
}
}
Outputs:
Python 2.7.18
{
"version": "2.0.0",
"tasks": [
{
"label": "Run some python",
"type": "shell",
"command": "pyenv activate projenv && python --version",´
}
}
Outputs on task run:
Failed to activate virtualenv.
Perhaps pyenv-virtualenv has not been loaded into your shell properly. Please restart current shell and try again.
In my ~/.zprofile I have:
echo "Hello .zprofile"
And in my ~/.zshrc I have:
echo "Hello .zshrc"
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
When running the tasks I get the "Hello .zprofile" but not the output from "Hello .zshrc" where the pyenv activation is. When I manually create a new VSCode terminal it outputs both.
Now if I duplicate or move the pyenv activation into .zprofile the pyenv virtual env activates and the task runs.
But why does VSCode not read both my .zprofile and .zshrc files to begin with?
To activate pyenv-virtualenv
with zsh place the eval "$(pyenv init -)"
inside the .zprofile
and not .zshrc
. The former gets executed when VSCode creates a terminal, the later does not.