Visual Studio Code terminal shows multiple Conda envs
Asked Answered
S

3

15

I have VSCode in Windows 11. I have WSL (Ubuntu 22.04) and launch VSCode from the terminal like code . from the project folder. When I open the built-in terminal it shows two conda (Anaconda) environments in parentheses, so I have no idea which one is active, if any. On subsequent conda deactivate you can see in the attached screenshot that the prompt and the active env changing but there is surely something messed up here.

Also, in VSCode, when I set Python Interpreter to a conda env, in a few seconds the built-in terminal prompt picks up the change and the env name in the first parens changes to the new value.

Any idea how to fix it?

(The prompt should obviously show just one (the active) conda env and that one should change whenever the Python interpreter is updated in the command palette.)

enter image description here

I looked into my ~/.bashrc file but there is just the seemingly normal >>> conda initialize block at the bottom that was added when installing Anaconda

Sympathize answered 2/11, 2023 at 15:31 Comment(3)
I have never seen such a bug before... Does VSCode automatically activate a virtual environment? You can disable the virtual environment activation in your VSCode settings.Sexed
@PéterSzilvási yeah, this was an exceptionally weird issue, and I have no idea how VSCode managed to get into this state, but I have a "fix", see belowSympathize
I just wanted to add this is an already known bug with vscode-python, for example reported here: #22834. It seems to be an UI issue, which is attempted to be fixed in #22128Wood
S
2

It turns out that I had to delete ~/.vscode-server directory and let it being autogenerated again on the next code . run. I also had to click the "Inherit Env" setting of the built-in Terminal (I forgot, it may not be the default).

With these steps, a newly launched VSCode behaves almost exactly like I expected:

  • If there was no built-in terminal open in the previous VSCode session, opening a new terminal looks like as in the bottom right tab: prompt starts with (base) env but immediately picks up correct end and returns correct prompt
  • if there was a built-in terminal open when killing the previous session, on the next launch the terminal will open automatically with the incorrect (base) env, see bottom left tab, but any subsequent new terminal added will look like the right window, which is now correct. This is still a bit inconvenient, having to either do a manual conda activate <correct_env> or close the terminal and open a fresh one, now with the correct conda env

anyways, I think I can live with this

enter image description here

Sympathize answered 2/11, 2023 at 21:52 Comment(2)
The Inherit Env settings is enabled by default. I would advise uninstalling VSCode and remove its cache folders. Then reinstall it freshly to avoid these unexpected bugs next time.Sexed
@PéterSzilvási Thanks, so it is enabled by default. I unticked and ticked it back a few times to see what it does and forgot its initial state. I think with purging the ~/.vscode-server folder from my WSL home folder I removed VSCode completely from WSL (I have not explicitly uninstall it from Windows 11, that's where VSCode originally lives on my PC). The next time I ran code from the WSL Ubuntu terminal it downloaded >1 GB data from the cloud, my profiles, extensions, etc., so I am all good now.Sympathize
L
26

I faced the same problem. Based on what I found on the Internet the Python extension for VSCode was the culprit.

As described in this page, the Python extension now automatically activates the environment, so it seems that the problem is caused by the automatic activation of .bashrc and the Python extension at the same time.

My solution is to execute conda config --set auto_activate_base False or turn off "Activate Python Environment in all Terminals created" setting in the Python extension configuration in VSCode.

Lustre answered 12/1 at 10:41 Comment(2)
This works! Is this issue result of the newest VS Code update? Before the update, I did not have this problem.Xanthate
This is it: conda config --set auto_activate_base FalseKanaka
S
2

It turns out that I had to delete ~/.vscode-server directory and let it being autogenerated again on the next code . run. I also had to click the "Inherit Env" setting of the built-in Terminal (I forgot, it may not be the default).

With these steps, a newly launched VSCode behaves almost exactly like I expected:

  • If there was no built-in terminal open in the previous VSCode session, opening a new terminal looks like as in the bottom right tab: prompt starts with (base) env but immediately picks up correct end and returns correct prompt
  • if there was a built-in terminal open when killing the previous session, on the next launch the terminal will open automatically with the incorrect (base) env, see bottom left tab, but any subsequent new terminal added will look like the right window, which is now correct. This is still a bit inconvenient, having to either do a manual conda activate <correct_env> or close the terminal and open a fresh one, now with the correct conda env

anyways, I think I can live with this

enter image description here

Sympathize answered 2/11, 2023 at 21:52 Comment(2)
The Inherit Env settings is enabled by default. I would advise uninstalling VSCode and remove its cache folders. Then reinstall it freshly to avoid these unexpected bugs next time.Sexed
@PéterSzilvási Thanks, so it is enabled by default. I unticked and ticked it back a few times to see what it does and forgot its initial state. I think with purging the ~/.vscode-server folder from my WSL home folder I removed VSCode completely from WSL (I have not explicitly uninstall it from Windows 11, that's where VSCode originally lives on my PC). The next time I ran code from the WSL Ubuntu terminal it downloaded >1 GB data from the cloud, my profiles, extensions, etc., so I am all good now.Sympathize
D
2

You may want to unregister the conda init script from bash.

Run conda init --reverse and restart the bash.

Dossier answered 5/4 at 15:58 Comment(2)
This worked but only for PowerShell, which is what the integrated terminal targets initially. As soon as I switch to cmd anaconda is active again :/Glochidium
@kyle-delaney On Windows, conda init defaults to both cmd.exe and powershell. (You can run conda init --help for more information.) Run conda info and the value of active environment should be None. However, if the environment is really active in your cmd, there may be a bug.Dossier

© 2022 - 2024 — McMap. All rights reserved.