Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is VS Code calling the wrong virtual environment as base?

I have some conda virtual environments in a server I SSH into in a daily basis. Logging in from the terminal and listing the environments gives me the following:

(base) [rgr6291@klc0201 ~]$ conda env list
# conda environments:
#
fomc                     /home/rgr6291/.conda/envs/fomc
r_conda                  /home/rgr6291/.conda/envs/r_conda
rfe_paper                /home/rgr6291/.conda/envs/rfe_paper
base                  *  /software/python-anaconda3/2019.10

So I have base and three virtual environments. I usually edit code on the server using Visual Studio Code and the Remote SSH extension. VS Code recognizes all these environments when I am prompted to select the Python interpreter:

enter image description here

It turns out that starting a terminal window from VS Code leads to different behavior whether I am on the fomc environment or not.

If I select, say, the rfe_paper environment and start a new terminal from withing VS Code, I get the following automatic output:

source activate rfe_paper
(base) [rgr6291@klc0201 HF_FOMC]$ source activate rfe_paper
(rfe_paper) [rgr6291@klc0201 HF_FOMC]$ 

It starts up the terminal and then activates the desired environment. However, the same procedure from the fomc environment leads to something different:

source /home/rgr6291/.conda/envs/fomc/bin/activate
(base) [rgr6291@klc0201 HF_FOMC]$ source /home/rgr6291/.conda/envs/fomc/bin/activate
(base) [rgr6291@klc0201 HF_FOMC]$ conda activate fomc

(base) [rgr6291@klc0201 HF_FOMC]$ conda env list
# conda environments:
#
base                  *  /home/rgr6291/.conda/envs/fomc
r_conda                  /home/rgr6291/.conda/envs/r_conda
rfe_paper                /home/rgr6291/.conda/envs/rfe_paper

Notice that there is an extra source call in the beginning. And somehow VS Code is misled and believes the fomc environment is the base one.

  1. What might be causing this?
  2. How to fix this?

It was working fine yesterday but it seems broken today. The only difference is that I exported the installed packages in fomc to an yml file this morning. I don't know how that would affect behavior.

By the way, let me report the following as well. This is the content of ~/.conda/envs/fomc/bin/activate:

#!/bin/sh
_CONDA_ROOT="/home/rgr6291/.conda/envs/fomc"
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
\. "$_CONDA_ROOT/etc/profile.d/conda.sh" || return $?
conda activate "$@"

I think CONDA_ROOT is wrong, but I have no idea of what it should actually be.

Thanks in advance.

like image 488
Raul Guarini Riva Avatar asked Oct 15 '25 16:10

Raul Guarini Riva


1 Answers

⚠️ Caution: This is an untested recommendation. Please make sure the whole procedure is clear before attempting. Be sure to back up any files before changing them!

I've not encountered this situation personally, but I suspect that resolving it will involve removing the conda package from the fomc environment. This may be sufficient; but it could also be important to ensure that the Conda initialization code in your .bashrc or .bash_profile is set up to use the actual base environment and not the scripts/entry points in the fomc environment.

So, I'd recommend first checking the .bashrc and .bash_profile and make sure they don't have any paths referencing the fomc. If they do, then the entire initialization block, starting and ending with

# >>> conda initialize >>>
...
# <<< conda initialize <<<

should be removed and then one should rerun the conda init command with the real base environment active, or using the full path to the base conda.

Once this is done, restart the shell and verify conda still works and points to the base (i.e., check which conda). Then attempt to remove the conda package from fomc, doing this without the fomc being active, i.e.,

conda remove -n fomc conda

That should be it.

like image 184
merv Avatar answered Oct 17 '25 05:10

merv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!