Activating Anaconda Environment in VsCode
Solution 1
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
Solution 2
Simply use
- shift + cmd + P
- 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.
Solution 3
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.
Solution 4
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 Workspace 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 Workspace Interpreter option.
Note: The Select Workspace Interpreter takes around 10 seconds to come up on my computer using the current version of VSCode.
Solution 5
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.
Comments
-
ctrlalt313373 over 2 years
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?
-
ctrlalt313373 about 7 yearsBut how do I activate a specific environment so that the script runs in that Anaconda environment?
-
eric_camplin about 7 yearsThe 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 -
Sathish over 6 yearsThanks for the answer. On Linux, it is
{ "python.pythonPath": "/<proper path>/anaconda3/envs/${env:CONDA_DEFAULT_ENV}/bin/python" }
. Also I have to re-open the VS Code after changing the environment. -
alexbhandari over 6 yearsvscode now has the
python.venvPath
setting that allows you to usePython: 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. -
Prayson W. Daniel over 6 yearsCtlr + Comma for Windows opens vscode settings :) Thank you.
-
Andy Longwill about 6 yearsOn Windows this worked for me: "python.venvPath": "C:\\Users\\User\\AppData\\Local\\Continuum\\anaconda3". Using VSCode 1.19.1
-
humble almost 6 yearsThis 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
-
Ryan over 5 yearsYeah 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.
-
BallpointBen over 5 yearsThis also doesn't seem to affect the environment used when running code cells in the Jupyter window
-
Joel Carneiro over 5 yearsThis way make it so easy :)
-
Chidi almost 5 yearsbest answer IMHO.
-
Hamidreza almost 5 yearsIt 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?
-
Wargream almost 5 years@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.
-
ColinMac almost 5 yearsOn more recent version the command is
Python: Select Interpreter
-
AnneTheAgile over 4 yearsThis 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 over 4 yearsI 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"
-
malthe over 4 years@HamidrezaAhmadi you ever solved this? I ran into this issue as well and tried following the advice on this page to no avail.
-
Hamidreza over 4 years@malthe Go to the
Anaconda Navigator
and in theHome
tab choose your venv right next toApplication on
label and then launch VS code from the Navigator (if instead oflaunch
option it's showinginstall
option, install it first, even if you have vs code installed on your pc) Just like what Zhenyu explained below. -
aderchox over 4 yearsI 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: docs.conda.io/projects/conda/en/latest/user-guide/tasks/… -
dumbledad about 4 years@Hamidreza & malthe On the 'Select and activate an environment' docs 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
-
ohailolcat almost 4 years*"shift+ctrl+p" for windows
-
Trect over 3 yearsWorks like charm
-
till Kadabra almost 3 yearsThe efficient's solution to the problem by far.
-
questionto42standswithUkraine over 2 years@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.
-
Gamrix over 2 yearsAn easy way to get the pythonpath on Mac/ Unix is to just run:
conda activate <your env> && which python
-
Titou about 2 yearsThis 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.