Visual Studio Code terminal doesn't activate Conda environment

Question:

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:Usersusername.condaenvstompython.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:

Enter image description here

But when I do conda env list even after doing conda activate tom in the terminal I get the output:

# conda environments:
#
base                  *  C:ProgramDataAnaconda3
tom                      C:Usersusername.condaenvstom

Instead of:

# conda environments:
#
base                     C:ProgramDataAnaconda3
tom                   *  C:Usersusername.condaenvstom

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'

Answers:

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.

Answered By: Gustav Rasmussen
  1. In Vscode hit ctrl+` to open your terminal.
  2. Then within the terminal type: conda init.
  3. Close and reopen the terminal.
  4. Use Conda normally.

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.

Answered By: Peter C

Activating anaconda virtual environment in vs code

  1. Go to the menu bar and click on Terminal.
  2. Type: conda init
  3. Close this terminal and open a new one
  4. In the new terminal window, Type conda info --envs
    (Your conda virtual env should be there in the list)
  5. Type conda activate name_of_venv
Answered By: Oorja Mathur

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:

  1. Press Shift + Command + P to open command palette.
  2. Type settings.json and select `Preferences: Open Settings (JSON)
  3. In the JSON file, add the key-value pair "terminal.integrated.inheritEnv": false
  4. Save the JSON file

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:UsersUSERDocumentsPowerShellprofile.ps1 with:

#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
(& "C:UsersUSERanaconda3Scriptsconda.exe" "shell.powershell" "hook") | Out-String | Invoke-Expression
#endregion

enter image description here

Then add the new PowerShell 7 terminal to vscode, see How to add anaconda powershell to vscode?.

The terminal menu:

enter image description here

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.

Answered By: questionto42

enter image description here

I was helped by switching the terminal from powershell to cmd

Answered By: user2281726

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.

Answered By: lorinlin

PythonPath is no longer used by the default Python plugin now (as of 2021). Instead, you can add the interpreter path using the following:

  1. In a terminal, run the following to get the path to your env:
conda activate <name of your env> && which python
  1. Then set the VSCode JSON setting (either user or workspace):
{
    "python.defaultInterpreterPath": "<Path to your env>",
}
  1. 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).

Answered By: Gamrix

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:

  1. ctrl+shift+p

  2. Type terminal: select default profile

  3. Choose Command Prompt.

    Image for 'choose Command Prompt'

  4. 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):

  1. On the right top of the terminal panel, click + (Launch Profiles)
  2. Select default Profile
  3. Choose which you want
    in this way, you can also open configure terminal settings

Solution 2:

  1. Do what Sabito 錆兎 said in their answer.

  2. ctrl+shift+p

  3. Type Python: Select Interpreter

  4. Choose which env in conda you need.
    You can also select on the bottom panel.

  5. 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 shows

    F:GitHubt>E:/Anaconda/Install/Scripts/activate 
    (base) F:GitHubt>conda activate base 
    (base) F:GitHubt>
    

    When I choose python3.8:condait shows

    F:GitHubt>E:/Anaconda/Install/Scripts/activate 
    (base) F:GitHubt>conda activate python3.8 
    (python3.8) F:GitHubt>
    

    PowerShell

    When I choose python3.8:conda. it shows

    (base) PS F:GitHubt> conda activate python3.8 
    (python3.8) PS F:GitHubt>
    

    When I choose base:conda, it shows

    (base) PS F:GitHubt> conda activate base  
    (base) PS  F:GitHubt>
    
Answered By: cooooopd
  1. Type: conda init
  2. switch to cmd terminal, because the shell terminal has problems
  3. enjoy

it’s work for me in vscode

Answered By: Yeison Aldana

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:
enter image description here

Answered By: owennewo

In my case when you:

  1. installed Python and set it to the Windows PATH
  2. refused to add Anaconda to PATHs as default Python interpreter during installation
  3. vs code finds the conda, but not conda cmdlet

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:

  1. Ctrl + Shift + P
  2. Preferences: Open User Settings (JSON)
  3. add the path to the location of your conda.exe, in my case it’s C:UsersUSERNAMEAppDataLocalanaconda3Scripts
 "terminal.integrated.env.windows": {
        "PATH": "C:\Users\USERNAME\AppData\Local\anaconda3\Scripts\;${env:PATH}"
    }

Specific workspace:

  1. Ctrl + Shift + P
  2. Preferences: Open Workspace Settings (JSON)
    "settings":
    {
        "terminal.integrated.env.windows": {
            "PATH": "C:\Users\USERNAME\AppData\Local\anaconda3\Scripts\;${env:PATH}"
        }
    }
Answered By: Alexey Militov