Caleb
Caleb

Reputation: 4091

PATH not updated correctly from conda activate in VSCode's terminal

I am using VSCodes terminal pane. I activate a conda environment. For some reason, the python command is still set to /usr/bin/python, instead of the correct path to the conda environment.

% conda activate myenv
% which python
/usr/bin/python

The correct anaconda environment directory does seem to be in the $PATH variable, but /usr/bin seems above it in priority.

When I open a standard terminal through the OS, the behavior is as I expect.

% conda activate myenv
% which python
/Users/cpl/anaconda3/envs/myenv/bin/python

Please note: I have already set the VSCode preferences key python.pythonPath to /Users/cpl/anaconda3/envs/myenv/bin/python, and I think that it works correctly. When I run a file through right-clicking and selecting Run Python File In Terminal, the correct python (from the conda environment) is executed. My problem is using the VSCode terminal directly to execute python.

My shell is zsh, and I am using OSX. Any advice?

Upvotes: 11

Views: 3402

Answers (4)

Enthus3d
Enthus3d

Reputation: 2165

If anyone else in the future ends up scratching their heads over this particular problem, I’ve found another culprit:

Showcase of setting Terminal>Integrated>Env: Enable Persistent Sessions

I suspect what happens is that after you update system paths, VScode caches the old path in the terminal and persists it. In this case it persists the old python path rather than the new conda one.

Toggling this option off and restarting VSCode clears that cache, and the new path is loaded in. You can also toggle the option back on after you’re done.

Upvotes: 1

Caleb
Caleb

Reputation: 4091

This behavior is explained in the VSCode docs: Why are there duplicate paths in the terminal's $PATH environment variable and/or why are they reversed?#

It sounds like VSCode will run your .zshrc twice in MacOS, conflicting with the conda-generated PATH variable definitions.

There are two solutions listed in the link above. The one that works for me is to set the VSCode setting "terminal.integrated.inheritEnv": false. The documentation warns that all of your environmental variables will be stripped if you do this. However, I find I still have my custom variables defined in the .zshrc file.

It is worth noting that recent versions of VSCode will prompt you when it detects you are using a conda environment, and suggests making this change.

Upvotes: 3

hongkail
hongkail

Reputation: 789

A dark magic below may work:-

In my Big Sur, I added the following empty entry to my settings.json -- can be found at File(Windows)/Code(Mac)>Preferences>Settings -- click on any link stated "Edit in settings.json"

"terminal.integrated.env.osx": {
        "PATH": ""
}

All the best!

Upvotes: 2

goodside
goodside

Reputation: 4629

I don't use zsh, but I've run into this issue in bash and I believe the cause is the same.

Conda has recently changed the "official" method of activating environments, as described in this issue: https://github.com/Microsoft/vscode-python/issues/1882

Before, you needed to modify your .bashrc/.zshrc to prepend PATH with the directory of conda's activate script, and then activate specific environments by typing source activate name_of_env. VSCode-Python activates conda terminals by sending this command to the shell — with visible echo, like you typed it yourself.

The new method is to source $HOME/anaconda3/etc/profile.d/conda.sh in .bashrc and then activate environments with conda activate name_of_env, which is the behavior you're seeing work correctly in a dedicated terminal. VSCode-Python does not yet support this, and there appear to be issues with cross-platform support on Windows that are complicating the transition.

The best solution for now is to ignore the "correct" method of conda activate and consistently use the older source activate name_of_env, which still works (if your PATH is set to include $HOME/anaconda3/bin).

Upvotes: 1

Related Questions