Search code examples
pythonvisual-studio-codecondaenvironment

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

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