Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'
like image 344
Sabito 錆兎 stands with Ukraine Avatar asked May 24 '20 12:05

Sabito 錆兎 stands with Ukraine


People also ask

How do you activate Anaconda environment in VS code?

Firstly, open the command palette using Ctrl + Shift + P. Secondly, Select Python: select Interpreter. Now, Select Enter interpreter path. We have to add the path where the env is, the default location will be C:\Users\YourUserName\Anaconda3\envs\env_name.

How do you activate environment in VS code?

Select and activate an environment# To select a specific environment, use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P).

How can I activate conda environment automatically?

Reload Window from Command Palette, select base:conda as python interpreter then press Ctrl+Shift+` to open a new integrated Terminal, conda environment should be activated automatically in it.

How to switch the Conda environment in VSCode?

Ctrl+Shift+P and search “ Terminal ”. In the Terminal window, you can see “conda activate <env>” command already executed, and the terminal is under your conda environment. The second way is to have manual control of switching the conda environment in the terminal. To achieve this, we need to make VSCode terminal recognize the “conda” commands.

How do I activate a Conda environment in Python?

One is to Create New Integrated Terminal (In Active Workspace) and the terminal would activate the conda environment based on the Python interpreter you specified in python.pythonPath. The other one is manually run “conda activate <env>” in the terminal. Ctrl+Shift+P and search “ Terminal ”.

How do I activate Conda from terminal?

The command to start the anaconda prompt in terminal should look something like this: C:<path-to-conda-installation>Scriptsactivate.bat C:<path-to-conda-installation>. This activates conda default environment, once you are in there you can change environment with the activatecommand: conda activate <your-env-name>.

How do I open Visual Studio Code in Anaconda?

First, open the Anaconda prompt ( How to access Anaconda command prompt in Windows 10 (64-bit) ), and type: To activate your virtual environment. Then to open Visual Studio Code in this active environment, type


4 Answers

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
like image 177
Charudatta Manwatkar Avatar answered Oct 23 '22 10:10

Charudatta Manwatkar


enter image description here

I was helped by switching the terminal from powershell to cmd

like image 27
user2281726 Avatar answered Oct 23 '22 11:10

user2281726


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.

like image 32
Gustav Rasmussen Avatar answered Oct 23 '22 10:10

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.
like image 19
Sabito 錆兎 stands with Ukraine Avatar answered Oct 23 '22 12:10

Sabito 錆兎 stands with Ukraine