'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:

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:\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:

  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

Solution 3:[3]

  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.

Solution 4:[4]

enter image description here

I was helped by switching the terminal from powershell to cmd

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

  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

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]

  1. Type: conda init
  2. switch to cmd terminal, because the shell terminal has problems
  3. 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

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.

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:

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

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:

  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:\GitHub\t>E:/Anaconda/Install/Scripts/activate 
    (base) F:\GitHub\t>conda activate base 
    (base) F:\GitHub\t>
    

    When I choose python3.8:condait shows

    F:\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>