Activating Anaconda Environment in VsCode

163,730

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

  1. shift + cmd + P
  2. Search Select Interpreter

pyhton : Select Interpreter

  1. Select it and it will show you the list of your virtual environment created via conda and other python versions

Activating conda virtual environment

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

Anaconda Navigator

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.

Share:
163,730
ctrlalt313373
Author by

ctrlalt313373

Software Developer in .NET

Updated on December 19, 2021

Comments

  • ctrlalt313373
    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
    ctrlalt313373 about 7 years
    But how do I activate a specific environment so that the script runs in that Anaconda environment?
  • eric_camplin
    eric_camplin about 7 years
    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
  • Sathish
    Sathish over 6 years
    Thanks 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
    alexbhandari over 6 years
    vscode 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.
  • Prayson W. Daniel
    Prayson W. Daniel over 6 years
    Ctlr + Comma for Windows opens vscode settings :) Thank you.
  • Andy Longwill
    Andy Longwill about 6 years
    On Windows this worked for me: "python.venvPath": "C:\\Users\\User\\AppData\\Local\\Continuum\\anaconda3". Using VSCode 1.19.1
  • humble
    humble almost 6 years
    This 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
    Ryan over 5 years
    Yeah 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
    BallpointBen over 5 years
    This also doesn't seem to affect the environment used when running code cells in the Jupyter window
  • Joel Carneiro
    Joel Carneiro over 5 years
    This way make it so easy :)
  • Chidi
    Chidi almost 5 years
    best answer IMHO.
  • Hamidreza
    Hamidreza almost 5 years
    It 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
    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
    ColinMac almost 5 years
    On more recent version the command is Python: Select Interpreter
  • AnneTheAgile
    AnneTheAgile over 4 years
    This 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
    AnneTheAgile over 4 years
    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"
  • malthe
    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
    Hamidreza over 4 years
    @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.
  • aderchox
    aderchox over 4 years
    I 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
    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
    ohailolcat almost 4 years
    *"shift+ctrl+p" for windows
  • Trect
    Trect over 3 years
    Works like charm
  • till Kadabra
    till Kadabra almost 3 years
    The efficient's solution to the problem by far.
  • questionto42standswithUkraine
    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
    Gamrix over 2 years
    An easy way to get the pythonpath on Mac/ Unix is to just run: conda activate <your env> && which python
  • Titou
    Titou about 2 years
    This 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.