I have Anaconda working on my system and VsCode working, but how do I get VsCode to activate a specific environment when running my python script?
-
10Most of the answers are windows specific. It would be nice to have an answer that includes MacOS and Linux systems. – Soerendip Nov 28 '17 at 10:21
15 Answers
Simply use
- cmd + shift + P (ctrl + shift + P for MS Windows)
- Search Select Interpreter
- Select it and it will show you the list of your virtual environment created via conda and other python versions
- select the environment and you are ready to go.
Quoting the 'Select and activate an environment' docs
Selecting an interpreter from the list adds an entry for
python.pythonPath
with
the path to the interpreter inside your Workspace Settings.
-
14This works. However, when I try to run the script using the CodeRunner extension, it doesn't seem to recognize that I am in a conda environment and gives me an error saying module doesnt exist – humble Aug 20 '18 at 12:36
-
4Yeah this doesn't seem to give the terminal session a path variable or set the system or user PATH variable, even temporarily in a virtual environment. – Ryan Dec 16 '18 at 10:29
-
This also doesn't seem to affect the environment used when running code cells in the Jupyter window – BallpointBen Jan 04 '19 at 19:19
-
-
-
2It works but there's another problem with my case! When I select the "python: select interpreter" option, it doesn't show me all the anaconda environments! Actually it just show 2 of them that I don't want. Have anyone any idea? – Hamidreza Jul 29 '19 at 10:48
-
@ragvri, modify first the output console of code-runner to the command prompt terminal. In the settings.json file, add this line "code-runner.runInTerminal": true. This will allow you to type-in on the output terminal of code-runner. You can just type "activate my_env" just like in a normal cmd prompt. – Wargream Aug 05 '19 at 07:46
-
@HamidrezaAhmadi you ever solved this? I ran into this issue as well and tried following the advice on this page to no avail. – malthe Dec 20 '19 at 15:06
-
1@malthe Go to the `Anaconda Navigator` and in the `Home` tab choose your venv right next to `Application on` label and then launch VS code from the Navigator (if instead of `launch` option it's showing `install` option, install it first, even if you have vs code installed on your pc) Just like what Zhenyu explained below. – Hamidreza Dec 20 '19 at 18:11
-
@Hamidreza & malthe On the 'Select and activate an environment' [docs](https://code.visualstudio.com/docs/python/environments#_activating-an-environment-in-the-terminal) they mention that "On Windows, it can take a little time for VS Code to detect available conda environments. During that process, you may see '(cached)' before the path to an environment." I did not see the 'cached' label but it did take a long time (and a VSCode restart) before I saw the conda environment I wanted to select – dumbledad Mar 05 '20 at 13:08
-
-
-
@Hamidreza I had to refresh the interpreter list with the round arrow on the upper right of the menu that pops up when you click on the Python interpreter in the status bar. – questionto42 Nov 11 '21 at 16:09
-
@Hamidreza Detecting conda environment should be automatic in vscode. However, I encountered the same issue as you. I had to uninstall vscode and anaconda following these instruction: https://code.visualstudio.com/docs/setup/uninstall, https://docs.anaconda.com/anaconda/install/uninstall/. After that, install vscode then anaconda (click the checkbox "Add PATH to VSCode..."). – Péter Szilvási Apr 03 '23 at 12:56
If Anaconda is your default Python install then it just works if you install the Microsoft Python extension.
The following should work regardless of Python editor or if you need to point to a specific install:
In settings.json edit python.path with something like
"python.pythonPath": "C:\\Anaconda3\\envs\\py34\\python.exe"
Instructions to edit settings.json

- 20,575
- 8
- 83
- 77

- 508
- 4
- 6
-
18But how do I activate a specific environment so that the script runs in that Anaconda environment? – ctrlalt313373 Apr 11 '17 at 18:34
-
The answer posted is how you run a .py file within VS Code using a specific environment like Anaconda. The path of my Anaconda install in the settings.json is `python.pythonPath": "C:\\Anaconda3\\envs\\py34\\python.exe"` note I updated to use "\\" to escape the single "\" in the path. Then right click on the .py code and choose "run file in python terminal" hope that helps – eric_camplin Apr 11 '17 at 20:58
-
10vscode now has the `python.venvPath` setting that allows you to use `Python: Select Workspace Interpreter` in the command palette to choose the env (see my answer for details). That way you don't have to edit the pythonpath every time you change envs. – alexbhandari Oct 30 '17 at 03:36
-
2This alone, settings.json, was not enough. It seemed to need also launch.json to get it working on OSX eg: "python.pythonPath": "/Users/me/miniconda2/envs/py27/bin/python" – AnneTheAgile Nov 22 '19 at 20:16
Setting python.pythonPath
in VSCode's settings.json
file doesn't work for me, but another method does. According to the Anaconda documentation at Microsoft Visual Studio Code (VS Code):
When you launch VS Code from Navigator, VS Code is configured to use the Python interpreter in the currently selected environment.

- 17,541
- 8
- 92
- 91

- 457
- 4
- 5
-
-
The conda path is set only if you launch the vscode using the anaconda navigator. The conda path settings will NOT be persisted after the vscode is closed. – Péter Szilvási Apr 03 '23 at 12:30
The best option I found is to set the python.venvPath
parameter in vscode settings to your anaconda envs folder.
"python.venvPath": "/Users/[...]/Anaconda3/envs"
Then if you bring up the command palette (ctl + shift + P
on windows/linux, cmd + shift + P
on mac) and type Python: Select Interpreter
all your envs will show up and you can select which env to use.
The python extension will also need to be installed for the Select Interpreter option.
Updated 05/2023: per ColinMac's comment: Select Workspace Interpreter
has changed to Select Interpreter

- 1,310
- 12
- 21
-
1Ctlr + Comma for Windows opens vscode settings :) Thank you. – Prayson W. Daniel Nov 11 '17 at 09:29
-
On Windows this worked for me: "python.venvPath": "C:\\Users\\User\\AppData\\Local\\Continuum\\anaconda3". Using VSCode 1.19.1 – Andy Longwill Mar 21 '18 at 11:21
-
7
Although approved answer is correct, I want to show a bit different approach (based on this answer).
Vscode can automatically choose correct anaconda environment if you start vscode from it. Just add to user/workspace settings:
{
"python.pythonPath": "C:/<proper anaconda path>/Anaconda3/envs/${env:CONDA_DEFAULT_ENV}/python"
}
It works on Windows, macOS and probably Unix. Further read on variable substitution in vscode: here.

- 16,763
- 6
- 27
- 35
-
2Thanks for the answer. On Linux, it is `{ "python.pythonPath": "/
/anaconda3/envs/${env:CONDA_DEFAULT_ENV}/bin/python" }`. Also I have to re-open the VS Code after changing the environment. – Sathish Sep 09 '17 at 05:28 -
1An easy way to get the pythonpath on Mac/ Unix is to just run: `conda activate
&& which python` – Gamrix Nov 16 '21 at 20:18
Unfortunately, this does not work on macOS. Despite the fact that I have export CONDA_DEFAULT_ENV='$HOME/anaconda3/envs/dev'
in my .zshrc and "python.pythonPath": "${env.CONDA_DEFAULT_ENV}/bin/python",
in my VSCode prefs, the built-in terminal does not use that environment's Python, even if I have started VSCode from the command line where that variable is set.

- 4,143
- 4
- 29
- 30
Just launch the VS Code
from the Anaconda Navigator
. It works for me.

- 5,350
- 5
- 35
- 72

- 3,933
- 2
- 26
- 33
Find a note here: https://code.visualstudio.com/docs/python/environments#_conda-environments
As noted earlier, the Python extension automatically detects existing conda environments provided that the environment contains a Python interpreter. For example, the following command creates a conda environment with the Python 3.4 interpreter and several libraries, which VS Code then shows in the list of available interpreters:
conda create -n env-01 python=3.4 scipy=0.15.0 astroid babel
In contrast, if you fail to specify an interpreter, as with conda create --name env-00, the environment won't appear in the list.

- 106
- 2
-
1I was just going to post the same answer! It's the only correct answer in some cases! The VSCode documentation explains clearly that conda environments that don't have a python module in them will NOT be listed in the list of interpreters by VSCode. So for example, if you've just created an empty conda virtual environment without any modules in it, you won't see it listed in VSCode. So I simply did `conda install --name my_env_name python` and my problem is fixed. For more on this command: https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-pkgs.html#installing-packages – aderchox Jan 02 '20 at 13:05
The simplest way to do it -
First open up terminal or command line and navigate to the project directory where you created the virtual environment.
Then activate the virtual environment with the command
conda activate venv_name
Once activated, in terminal type -
code .
This will open the vscode with the activated virtual environment. Look at the bottom of the pic. The dot after code .
tells terminal to open the current directory in vscode.

- 675
- 7
- 22
-
1This did not deserve to be downvoted. It is a workaround, but having a working environment is the basis in debugging the configuration problem asked byt the OP. – Titou Mar 23 '22 at 11:18
-
For macOS if you get "command not found: code" you may need to type Cmd ⌘ + Shift ⇧ + P to open the Command Palette, type in "shell command" and select the Shell command: Install ‘code’ command in PATH to install it. – Tom J Nov 06 '22 at 03:38
If you need an independent environment for your project: Install your environment to your project folder using the --prefix option:
conda create --prefix C:\your\workspace\root\awesomeEnv\ python=3
In VSCode launch.json configuration set your "pythonPath" to:
"pythonPath":"${workspaceRoot}/awesomeEnv/python.exe"

- 319
- 2
- 4
-
I didn't need a separate environment, but to use my normal conda env on osx, I needed to redirect the pythonPath. The settings.json seemed to control properly the unit tests inside IDE . To get debugging to work though, I needed to also set pythonPath inside launch.json as this answer says. For me that was; "python.pythonPath": "/Users/me/miniconda2/envs/py27/bin/python" – AnneTheAgile Nov 22 '19 at 20:21
Python Path
is now deprecated and now you should set Conda Path
instead. This way you can pick different environements on the fly.
Click ctrl + ,
then search for Conda Path
and add absolute path to script, e.g.:
C:\Users\{myUser}\miniconda3\Scripts\conda.exe
Pick specific environment for each project in bottom left corner or through Command Pallete (ctrl + Shift + P
-> search Python: Select Interpreter
)

- 640
- 1
- 12
- 38
I found out that if we do not specify which python version we want the environment which is created is completely empty. Thus, to resolve this issue what I did is that I gave the python version as well. i.e
conda create --name env_name python=3.6
so what it does now is that it installs python 3.6 and now we can select the interpreter. For that follow the below-mentioned steps:
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
Finally, you have successfully activated your environment. It might now be the best way but it worked for me. Let me know if there is any issue.

- 84
- 1
- 12
I found a hacky solution replace your environment variable for the original python file so instead it can just call from the python.exe from your anaconda folder, so when you reference python it will reference anaconda's python.
So your only python path in env var should be like:
"C:\Anaconda3\envs\py34\", or wherever the python executable lives
If you need more details I don't mind explaining. :)

- 17,541
- 8
- 92
- 91

- 364
- 2
- 9
As I was not able to solve my problem by suggested ways, I will share how I fixed it.
First of all, even if I was able to activate an environment, the corresponding environment folder was not present in C:\ProgramData\Anaconda3\envs
directory.
So I created a new anaconda environment using Anaconda prompt,
a new folder named same as your given environment name will be created in the envs
folder.
Next, I activated that environment in Anaconda prompt.
Installed python with conda install python
command.
Then on anaconda navigator, selected the newly created environment in the 'Applications on' menu. Launched vscode through Anaconda navigator.
Now as suggested by other answers, in vscode, opened command palette with Ctrl + Shift + P
keyboard shortcut.
Searched and selected Python: Select Interpreter
If the interpreter with newly created environment isn't listed out there, select Enter Interpreter Path
and choose the newly created python.exe which is located similar to C:\ProgramData\Anaconda3\envs\<your-new-env>\
.
So the total path will look like C:\ProgramData\Anaconda3\envs\<your-nev-env>\python.exe
Next time onwards the interpreter will be automatically listed among other interpreters.
Now you might see your selected conda environment at bottom left side in vscode.

- 37
- 1
- 7
"python.pythonPath" is deprecated, quote from vscode:
The "python.pythonPath" setting in your settings.json is no longer used by the Python extension. If you want, you can use a new setting called "python.defaultInterpreterPath" instead. Keep in mind that you need to change the value of this setting manually as the Python extension doesn’t modify it when you change interpreters. Learn more.
Thus, IF you want to assign the path manually (not reccommended, as explained above), open the "settings.json" of your workspace or the default one and use
{
"python.defaultInterpreterPath": "C:\\Users\\MYUSER\\anaconda3\\envs\\testenv\\python.exe"
}

- 7,175
- 4
- 57
- 90