Working with Anaconda in Visual Studio Code

112,345

Solution 1

Activating a conda environment does not place conda on your PATH. You need to launch the Anaconda Prompt app from your Start menu to get a command-line with conda on your PATH if you didn't check the box to include conda during installation.

Also realize that conda only supports PowerShell as of conda 4.6 which was released in January 2019.

And the Python extension for VS Code works with conda fine. Create a conda environment and the extension will allow you to select it as your environment/interpreter.

Solution 2

I beat my head on this for far too long... launching VS Code from an Anaconda Prompt both feels clunky and didn't really work for integrated Powershell terminals (the default VS Code integrated terminal on Windows)

I wanted to be able to launch VS Code from any prompt (and usually the Windows menu shortcut) and still interact with Conda.

These two methods both worked for Anaconda 2020.02, VS Code 1.44.0, on Windows 10.

Easy & Quick

Switch VS Code to use cmd.exe as the default integrated terminal shell by:

  • opening the command palette (Control-Shift-P)
  • search for Terminal: Select Default Profile
  • select Command Prompt

Harder / Powershell

  • add the location of conda to your PATH (if you did not add it via the installer). For me on an "All Users" install this is C:\ProgramData\Anaconda\Scripts
  • from an Administrator Powershell prompt change the Powershell Execution Policy to remote signed i.e. Set-ExecutionPolicy RemoteSigned
  • open an Anaconda Prompt and run conda init powershell which will add Conda related startup to a Powershell profile.ps1 somewhere in your user's profile.

When you run VS Code and either debug a .py or launch a Powershell integrated terminal you should now see a message about "Loading personal and system profiles" and the Conda environment being prefixed on the terminal's prompt.

Solution 3

For me, this solution worked in VSC (1.40) ->

1.Set the Interpreter to Python 3.7.1 ('base':conda)

2. Rather than using PowerShell I switched (Select Default Shell) to Command Prompt and started a New Terminal -> now it's in conda (base) environment.

[for Anaconda Distribution, 2018.12]

Solution 4

To set anaconda prompt as your default terminal in VScode:

  1. (type) CTRL + SHIFT + P
  2. (search for:) open settings
  3. (click:) Preferences: Open Settings (JSON)

Then add three line configuration:

{
    ... # any other settings you have already added (remove this line)

    "terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\cmd.exe",
    "terminal.integrated.shellArgs.windows": ["/K", "C:\\Anaconda3\\Scripts\\activate.bat C:\\Anaconda3"],
    "python.condaPath": "C:\\Anaconda3\\Scripts\\conda.exe"
}

Finally, Restart your VScode

Solution 5

Following the trails of @andre-barbosa and @kenlukas, the below configuration works well for me (PowerShell on Windows 10):

"terminal.integrated.shellArgs.windows": [
    "-ExecutionPolicy",
    "ByPass",
    "-NoExit",
    "-Command",
    "your-path-to-\\conda-hook.ps1",
    ";conda activate 'your-path-to-the-base-conda-environment'"
]

Substitute "your-path-to-\conda-hook.ps1" and "your-path-to-the-base-conda-environment" with your actual path, and add it to VSCode's settings.json. Check out the properties of your "Anaconda Powershell Prompt" shortcut and you'll find what you need.

The key is really to obey the rules of VSCode's JSON configuration file, by turning the whole arguments string into a comma separated list.

Share:
112,345
BernardL
Author by

BernardL

ive reached a thousand rep - 22nd Oct 2018

Updated on July 05, 2022

Comments

  • BernardL
    BernardL almost 2 years

    I am getting a bit confused here, the latest Anaconda Distribution, 2018.12 at time of writing comes with an option to install Microsoft Visual Studio Code, which is great.

    When launching VSC and after Python: Select Interpreter and with a fresh install of Anaconda, I can see ~Anaconda3\python.exe which I assume is the Anaconda Python Environment, however, when I try to run some commands, I am getting:

    PS ~\Documents\Python Scripts\vs> ~/Anaconda3/Scripts/activate
    PS ~\Documents\Python Scripts\vs> conda activate base
    

    conda : The term 'conda' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1

    Now I know that it might be related to the environment variables but I find it highly odd as during the Anaconda installation, there are specific mentions that it is not required to add the Anaconda path to the environment variables. However after the error, the integrated terminal manages to launch Python and I am able to run code.

    Next in line is that I am unable to view any variables in the debugger after running a simple script, as shown in the tutorial here:

    msg = "Hello World"
    print(msg)
    

    I do expect to see similar results as shown in the link such as the dunder variables, I have also updated my launch.json with stopOnEntry = True following the steps.

    I would like to know if it is possible to use Visual Studio Code with Anaconda as a interpreter without registering variables from the original distribution and if I am missing out anything required.

    I expected the experience to be more straight forward but also I might be missing something, I am running on Windows 10.

  • BernardL
    BernardL about 5 years
    I figure out as much, thought that there might have been some magic functions working behind that temporarily adds to PATH. So if I understand correctly, the method that works is to launch VS Code via Anaconda Prompt/Navigator > Select interpreter > Select Environment and code away?
  • Brett Cannon
    Brett Cannon about 5 years
    If you want to have conda in the terminal, then yes, that should work. If you don't care about having conda in the integrated terminal then simply launch VS Code as normal and you can always open the Anaconda Prompt next to VS Code.
  • Michael Kopp
    Michael Kopp about 4 years
    I had to add -ExecutionPolicy ByPass to avoid errors about execution policies. See stackoverflow.com/a/61402982/2165903
  • Michael Kopp
    Michael Kopp about 4 years
    Can you tell me me how to configure the python Debugger (extension ms-python.python) such that this works as well? Currently it seems like conda is found in my Python Terminal, but not in my Python Debug Console Terminal.
  • Benny
    Benny almost 4 years
    @MichaelKopp Not sure if this is possible. I played around with the debug config file launch.json a little, and it seems that the console field is constrained to one of the three values: internalConsole, integratedTerminal, and externalTerminal, and does not accept additional arguments.
  • Sam Murphy
    Sam Murphy over 3 years
    if you installed Miniconda and/or as a User instead of Administrator then remember to update the shellArgs and condaPath appropriately. To find the .bat and .exe you can run 'where conda' in an Anaconda prompt outside of VS code. The other shellArg is the installation path for Anaconda or Miniconda
  • Jingpeng Wu
    Jingpeng Wu over 3 years
    I can not find the Command Prompt option. I am using ``` Version: 1.51.0 Commit: fcac248b077b55bae4ba5bab613fd6e9156c2f0c Date: 2020-11-05T18:14:40.758Z (3 wks ago) Electron: 9.3.3 Chrome: 83.0.4103.122 Node.js: 12.14.1 V8: 8.3.110.13-electron.0 OS: Darwin x64 19.6.0 ```
  • janh
    janh over 3 years
    @JingpengWu if I am reading your versions right, it looks like you are trying to do this on a Mac? Command Prompt only applies to Windows afaik
  • Jingpeng Wu
    Jingpeng Wu over 3 years
    thanks for checking this. Yes, I am using it on a Mac.
  • user32882
    user32882 about 3 years
    I'd argue that if you have to launch vscode itself from within the conda environment to properly define all the PATH variables there shouldn't be a need to actually select the interpreter from within vscode. The location of the appropriate python executable is already defined in the conda environment. This is definitely a flaw in vscode design. There should be a way to provide full conda support without bogging the user down by having them start an anaconda terminal and open vscode from there for everything to work...
  • Brett Cannon
    Brett Cannon about 3 years
    @user32882 no one is arguing with you on that and we are actively working on improving it (the new code is already out for A/B experiment and if you use our insiders build you can get it right now). But also note that making conda work is not simple for various reasons that SO's character count limit makes hard to get into. But do know we are doing the best we can.
  • Chen Lizi
    Chen Lizi about 3 years
    This is the only solution that works for me. Thank you!@Zhongyi.
  • Sergio
    Sergio almost 3 years
    as of 05.21, "Select default shell' was changed to 'Select Default Profile'
  • janh
    janh almost 3 years
    @Sergio thx, updated the answer. i do feel this answer is getting slowly out of date as there are a lot of changes afoot in the way Python and Vscode interact!
  • BenB
    BenB almost 3 years
    What are the implications of allowing "RemoteSigned"? Is there a way to limit this to the scripts required for getting Conda to work in VSCode's Powershell?
  • Wing Lee
    Wing Lee over 2 years
    great answer, and do not forget to set powershell execution policy to "remote signed". Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
  • Jordan M
    Jordan M over 2 years
    Noob question - why?
  • Neits
    Neits over 2 years
    I am trying this in VS Code 1.60 and now it requires the "args" to be an array. However, when I enclose the string in [] I get an error saying "The argument '-ExecutionPolicy ByPass -NoExit... is not recognized as the name of a script file".
  • kym
    kym over 2 years
    Worked perfectly for me on Windows 11 as of 10/10/2021. Thanks!
  • CodingButStillAlive
    CodingButStillAlive over 2 years
    Thanks! This really helped me a lot.
  • xagg
    xagg about 2 years
    @Neits it should be the command line that executes your anaconda prompt from powershell. Naylinn suggests to get it from the properties of your Anaconda prompt application (find icon, right click then properties and target)
  • Jeff Ellen
    Jeff Ellen almost 2 years
    I didn't read before starting, sadly, you cannot chose the 'harder' option (powershell) if you don't have admin rights on the windows box.
  • Adji_PA
    Adji_PA almost 2 years
    Though the same as naylinn, I prefer this solution due to its readability. Using Anaconda, however, the args didn't work fully for me, and I didn't realize that the %USERNAME% wildcard didn't work stackoverflow.com/questions/63591046/… For the last args element above, I'd have this instead: "-Command", "'& 'C:\\Users\\${env:USERNAME}\\Anaconda3\\shell\\condabin\\con‌​da-hook.ps1' ; conda activate 'C:\\Users\\${env:USERNAME}\\Anaconda3''"
  • Gaurav Kumar
    Gaurav Kumar almost 2 years
    @Adji_PA your comment helped me!