Reputation: 5065
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'
Upvotes: 57
Views: 137123
Reputation: 277
You can simply use the conda.bat
file to activate the conda manually.
<path_to_conda.bat_file> activate
The base environment should be activated.
path_to_conda.bat : <conda installed>\condabin\conda.bat
Upvotes: 0
Reputation: 20386
My problem was that conda activates the base environment by default.
conda config --set auto_activate_base false
I disabled it and it works
Upvotes: 0
Reputation: 1
Adding conda path to Environment Variables works
Steps to add Conda path to Env Variables for windows:
installation location
for your Anaconda/Miniconda3
.condabin
folder (this folder is usually on top only).Environment Variables
in Windows search and open open Edit Environment Variables for your account
.Environment Variables
window under User Variables
open Path
.Upvotes: 0
Reputation: 612
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:
Shift + Command + P
to open command palette.Preferences: Open Settings (JSON)
"terminal.integrated.inheritEnv": false
Upvotes: 47
Reputation: 1038
VS Code now allows you to make a new terminal profile which helps with conda on Windows: https://code.visualstudio.com/docs/terminal/profiles
Basically, you open up settings.json
and just add the following:
"Miniconda": {
"path": "C:\\Windows\\System32\\cmd.exe",
"args": ["/K", "C:\\Users\\$USER\\miniconda3\\Scripts\\activate.bat C:\\Users\\${env:USERNAME}\\miniconda3"],
"icon": "terminal-cmd"
}
You can then set it as the default profile.
Upvotes: 0
Reputation: 41
Switching the terminal from powershell to git bash or command prompt fixed it for me.
Upvotes: 0
Reputation: 365
The focus of this answer is to get the conda python environments to work with the Python Environments extension in vs code. There are many ways to do this, each has different effects and implications on how you want to interact with Python in vs code.
My Environment: Windows 10, Conda 23.7.4, Conda installed in C:\apps & I am using PowerShell 7 not windows PowerShell.
code
in conda prompt. This loads Conda environments in to Python Environments window. Includes nice ui conponents. This also removes conda from the powershell terminal. So you can write "activate myenv"."python.condaPath": "C:\\apps\\Anaconda3\\Scripts\\",
"python.venvPath": "C:\\apps\\Anaconda3\\envs",
"terminal.integrated.defaultProfile.windows": "Command Prompt",
This method puts conda environments in Global and has another folder for Venv.
conda init
& add to settings:"python.condaPath": "C:\\apps\\Anaconda3\\Scripts\\",
"python.venvPath": "C:\\apps\\Anaconda3\\envs",
Notes:
code
and navigatorUpvotes: 0
Reputation: 9512
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
.
Remark from a comment: you will likely need conda init
to update your PowerShell 7
profile.
Upvotes: 2
Reputation: 461
In my case simply choosing "Command Prompt" instead of "Poweshell" as VS-code terminal worked. I am not sure if there are any disadvantages of using command prompt over Powershell because I am not a heavy windows user. Not for python programming at least. I will be exploring more and will add edits later if I found something.
Upvotes: 0
Reputation: 41
In my case when you:
Solution:
Let vs code add the conda directory as the first search path in Windows PATH for you when it is running. This can be done both for the whole vs code and for a specific workspace.
Entire vs code:
"terminal.integrated.env.windows": {
"PATH": "C:\\Users\\USERNAME\\AppData\\Local\\anaconda3\\Scripts\\;${env:PATH}"
}
Specific workspace:
"settings":
{
"terminal.integrated.env.windows": {
"PATH": "C:\\Users\\USERNAME\\AppData\\Local\\anaconda3\\Scripts\\;${env:PATH}"
}
}
Upvotes: 0
Reputation: 381
There seems to be many different causes for conda not being automatically activated. I'm using python in wsl2 with bash as my shell and setting the following two settings to true fixed it for me:
Upvotes: 0
Reputation: 31
it's work for me in vscode
Upvotes: 3
Reputation: 111
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):
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:
When I choose base:conda
, I open a new terminal. In the console, it shows
F:\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 shows
F:\GitHub\t>E:/Anaconda/Install/Scripts/activate
(base) F:\GitHub\t>conda activate python3.8
(python3.8) F:\GitHub\t>
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>
Upvotes: 9
Reputation: 892
PythonPath
is no longer used by the default Python plugin now (as of 2021). Instead, you can add the interpreter path using the following:
conda activate <name of your env> && which python
{
"python.defaultInterpreterPath": "<Path to your env>",
}
(All this assumes that there are no other messed up settings in your vscode. If there are, look at other answers to this question).
Upvotes: 0
Reputation: 9
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.
Upvotes: 0
Reputation: 349
I was helped by switching the terminal from powershell to cmd
Upvotes: 23
Reputation: 51
Activating anaconda virtual environment in vs code
conda init
conda info --envs
conda activate name_of_venv
Upvotes: 3
Reputation: 2287
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.
Upvotes: 1
Reputation: 5065
conda init
.Upvotes: 38
Reputation: 3961
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.
Upvotes: 52