'Visual Studio Code terminal doesn't activate Conda environment
I read this Stack Overflow post on a similar issue, but the suggestions there don't seem to be working. I installed Visual Studio Code on my Windows machine and added the Python extension. Then I changed the Python path for my project to C:\Users\username\.conda\envs\tom\python.exe
. The .vscode/settings.json
has this in it:
{
"python.pythonPath": "C:\\Users\\username\\.conda\\envs\\tom\\python.exe"
}
The status bar in Visual Studio Code also shows:
But when I do conda env list
even after doing conda activate tom
in the terminal I get the output:
# conda environments:
#
base * C:\ProgramData\Anaconda3
tom C:\Users\username\.conda\envs\tom
Instead of:
# conda environments:
#
base C:\ProgramData\Anaconda3
tom * C:\Users\username\.conda\envs\tom
Also the packages not installed in base don't get imported when I try python app.py
. What should I do?
where python
runs, but it doesn't give any output.
Also,
import os
import sys
os.path.dirname(sys.executable)
gives
'C:\\Python38'
Solution 1:[1]
First, open the Anaconda prompt (How to access Anaconda command prompt in Windows 10 (64-bit)), and type:
conda activate tom
To activate your virtual environment.
Then to open Visual Studio Code in this active environment, type
code
And it should work.
Solution 2:[2]
I was facing the same issue for a long time and nothing seemed to work. Out of nowhere, VS Code suggested me the following in a notification prompt:
We noticed you're using a conda environment. If you are experiencing issues with this environment in the integrated terminal, we recommend that you let the Python extension change "terminal.integrated.inheritEnv" to false in your user settings.
I clicked yes
on it and worked. You can also manually set this in your settings.json as follows:
- Press
Shift + Command + P
to open command palette. - Type settings.json and select `Preferences: Open Settings (JSON)
- In the JSON file, add the key-value pair
"terminal.integrated.inheritEnv": false
- Save the JSON file
Solution 3:[3]
- In Vscode hit ctrl+` to open your terminal.
- Then within the terminal type:
conda init
. - Close and reopen the terminal.
- Use Conda normally.
Solution 4:[4]
Solution 5:[5]
This seems to be because the Anaconda installation recommends not changing the windows PATH to reference anaconda, as it can clash with other installations, I had no problems when my PATH variable was modified. It seems like vs code (or most likely the Python extension) is still not activating anaconda correctly itself.
Solution 6:[6]
Activating anaconda virtual environment in vs code
- Go to the menu bar and click on Terminal.
- Type:
conda init
- Close this terminal and open a new one
- In the new terminal window, Type
conda info --envs
(Your conda virtual env should be there in the list) - Type
conda activate name_of_venv
Solution 7:[7]
The virtual environment can be activated in the VSCode terminal, but the Python version is not switched.
The solution is to select “bash” in VSCode, everything is alright.
Solution 8:[8]
- Type: conda init
- switch to cmd terminal, because the shell terminal has problems
- enjoy
it's work for me in vscode
Solution 9:[9]
If conda init
fails, it might not just be a problem of conda missing in the search path. In my case it was because PowerShell did not have the rights to load the profile.ps1
, see The term 'conda' is not recognized as the name of a cmdlet.
In that case, and if you have not yet done so, install PowerShell 7
in parallel to PowerShell 5
and fill the C:\Users\USER\Documents\PowerShell\profile.ps1
with:
#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
(& "C:\Users\USER\anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | Invoke-Expression
#endregion
Then add the new PowerShell 7 terminal to vscode, see How to add anaconda powershell to vscode?.
The terminal menu:
And conda init
and the activation of the chosen Python interpreter's environment that you see in the status bar of vscode will run automatically any time you open PowerShell 7
.
Solution 10:[10]
PythonPath
is no longer used by the default Python plugin now (as of 2021). Instead, you can add the interpreter path using the following:
- In a terminal, run the following to get the path to your env:
conda activate <name of your env> && which python
- Then set the VSCode JSON setting (either user or workspace):
{
"python.defaultInterpreterPath": "<Path to your env>",
}
- Restart VSCode completely to see your env in the terminal, as it seems that VSCode sometimes caches the terminal window.
(All this assumes that there are no other messed up settings in your vscode. If there are, look at other answers to this question).
Solution 11:[11]
I've already tried most solutions to solve this problem. However, those didn't work. Following the steps that Sabito ?? mentioned in their answer, I finally solved it.
Solution 1:
ctrl+shift+p
Type
terminal: select default profile
Choose Command Prompt.
Open a new terminal and you can use cmd to do such things.
Another way to change the default terminal is (in case you forget the command):
- On the right top of the terminal panel, click + ? (Launch Profiles)
- Select default Profile
- Choose which you want in this way, you can also open configure terminal settings
Solution 2:
Do what Sabito ?? said in their answer.
ctrl+shift+p
Type
Python: Select Interpreter
Choose which env in conda you need. You can also select on the bottom panel.
Unnecessary step: in
vscode-settings.json
, make sure that if this line exists then the argument is true:"python.terminal.activateEnvironment": true,
Now, vscode will automatically change your env in your terminal according to your choice, like this:
CMD
When I choose
base:conda
, I open a new terminal. In the console, it showsF:\GitHub\t>E:/Anaconda/Install/Scripts/activate (base) F:\GitHub\t>conda activate base (base) F:\GitHub\t>
When I choose
python3.8:conda
it showsF:\GitHub\t>E:/Anaconda/Install/Scripts/activate (base) F:\GitHub\t>conda activate python3.8 (python3.8) F:\GitHub\t>
PowerShell
When I choose
python3.8:conda
. it shows(base) PS F:\GitHub\t> conda activate python3.8 (python3.8) PS F:\GitHub\t>
When I choose
base:conda
, it shows(base) PS F:\GitHub\t> conda activate base (base) PS F:\GitHub\t>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow