105

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.

Brett Cannon
  • 14,438
  • 3
  • 45
  • 40
BernardL
  • 5,162
  • 7
  • 28
  • 47

21 Answers21

168

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:

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

Harder / Powershell

  1. 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
  2. from an Administrator Powershell prompt change the Powershell Execution Policy to remote signed i.e. Set-ExecutionPolicy RemoteSigned, or from a non-admin Powershell prompt change the Powershell Execution Policy to remote signed i.e. Set-ExecutionPolicy -Scope CurrentUser RemoteSigned
  3. 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.

janh
  • 1,926
  • 2
  • 11
  • 13
  • 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 ``` – Jingpeng Wu Nov 30 '20 at 01:29
  • @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 – janh Dec 13 '20 at 10:55
  • thanks for checking this. Yes, I am using it on a Mac. – Jingpeng Wu Dec 14 '20 at 15:01
  • as of 05.21, "Select default shell' was changed to 'Select Default Profile' – Sergio May 27 '21 at 07:37
  • @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! – janh Jun 10 '21 at 04:55
  • 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? – BenB Jun 13 '21 at 17:00
  • 2
    Worked perfectly for me on Windows 11 as of 10/10/2021. Thanks! – kym Oct 10 '21 at 19:09
  • 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. – Jeff Ellen May 06 '22 at 21:06
  • Does the harder (powershell) option has any advantages? – Yousuf Azad Nov 07 '22 at 10:54
  • Still works as a charm on Windows 10 as of 21/06/2023. Ty! – Maarten Jun 20 '23 at 23:11
  • Thanks @janh. I followed your instructions for Powershell set up on my Win11 and 'conda' now works as desired. – Dennis Yang Jul 08 '23 at 00:04
  • My problem was on the contrary: my powershell activated the base env successfully and add the path to conda automatically. It caused the problem that prevent gdb from launching when I debug C++ code (because anaconda put its own mingw path at the front). I have to use cmd only. It there any way to have both coexist? – jerron Aug 14 '23 at 00:51
57

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]

m_kos
  • 1,609
  • 1
  • 9
  • 7
32

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

Zhongyi
  • 381
  • 4
  • 5
  • 5
    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 – Sam Murphy Nov 27 '20 at 10:53
  • This is the only solution that works for me. Thank you!@Zhongyi. – Chen Lizi Apr 25 '21 at 02:01
  • Unfortunately for me, these lines give hover message "unsupported property". – Mark Seagoe Sep 29 '22 at 03:05
  • VSCode tells me that this options are deprecated and that I should create a terminal profile. The solution of "blaylockbk" adds the profile to the settings – ChesuCR Nov 11 '22 at 12:34
20

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.

Brett Cannon
  • 14,438
  • 3
  • 45
  • 40
  • 5
    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? – BernardL Feb 26 '19 at 01:45
  • 1
    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. – Brett Cannon Feb 26 '19 at 23:25
  • 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... – user32882 Apr 12 '21 at 09:47
  • 1
    @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. – Brett Cannon Apr 16 '21 at 19:58
15

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.

Benny
  • 847
  • 1
  • 10
  • 14
  • 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. – Michael Kopp Apr 28 '20 at 20:48
  • @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. – Benny May 08 '20 at 04:24
  • This saved me a lot of time. Can confirm it still works and resolved an issue I had with VS Code and mambaforge on Windows 11. The initial setting in the json file was wrongly formatted, not as an array. – Dr_Be Jun 09 '23 at 19:24
10

Adding this setting to settings.json solved for me:

"terminal.integrated.shellArgs.windows": [
       "PowerShell -NoExit -File C:\\ProgramFiles\\Anaconda\\shell\\condabin\\conda-hook.ps1"
    ]

PS: This answer is based on kenlukas's answer, but for powershell instead of cmd.

Andre Barbosa
  • 142
  • 1
  • 8
  • 2
    I had to add `-ExecutionPolicy ByPass` to avoid errors about execution policies. See https://stackoverflow.com/a/61402982/2165903 – Michael Kopp Apr 28 '20 at 12:20
9

If you launch VS Code application from inside the Anaconda Navigator application, you automatically get a "conda-aware" VSCode integrated terminal.

This way you don't need to change the settings in VS Code itself.

Aung Htet
  • 711
  • 8
  • 6
8

For me, this article solved my issue (on Windows 10): Specifically, the settings.json entry: terminal.integrated.shellArgs.windows e.g.:

    "terminal.integrated.shellArgs.windows": [
        "/K", 
        "C:\\Programs\\Anaconda3\\Scripts\\activate.bat & conda activate py37"
    ]

It works well with command line but doesn't seem to work with PowerShell.

kenlukas
  • 3,616
  • 9
  • 25
  • 36
PhoenixQ
  • 123
  • 1
  • 2
  • 5
8

The "terminal.integrated.shellArgs.windows" method is depreciated:

This is deprecated, the new recommended way to configure your default shell is by creating a terminal profile in #terminal.integrated.profiles.windows# and setting its profile name as the default in #terminal.integrated.defaultProfile.windows#.

After reading the VS code docs (configuring profiles), I created a new profile for the Anaconda Prompt. The following is what is in my settings.json file:

"terminal.integrated.profiles.windows":{
    "PowerShell": {
        "source": "PowerShell",
        "icon": "terminal-powershell"
    },
    "Command Prompt": {
        "path": [
            "${env:windir}\\Sysnative\\cmd.exe",
            "${env:windir}\\System32\\cmd.exe"
        ],
        "args": [],
        "icon": "terminal-cmd"
    },
    "Git Bash": {
        "source": "Git Bash"
    },
    "Anaconda Prompt": {
        "source": "PowerShell",
        "args": [
            "powershell",
            "-NoExit",
            "-ExecutionPolicy ByPass",
            "-NoProfile",
            "-File C:\\path\\to\\Miniconda3\\shell\\condabin\\conda-hook.ps1"
        ],
        "icon": "smiley"
    }
}
blaylockbk
  • 2,503
  • 2
  • 28
  • 43
  • 1
    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 https://stackoverflow.com/questions/63591046/vs-code-username-wild-card-in-json-settings For the last args element above, I'd have this instead: "-Command", "'& 'C:\\Users\\${env:USERNAME}\\Anaconda3\\shell\\condabin\\conda-hook.ps1' ; conda activate 'C:\\Users\\${env:USERNAME}\\Anaconda3''" – Adji_PA May 15 '22 at 09:01
  • @Adji_PA your comment helped me! – Gaurav Kumar May 27 '22 at 09:18
5

Command Prompt (default) stopped working for me Sept 2021

Originally I used the quick fix that many have suggested:

Terminal dropdown --> Select default profile --> Command Prompt

Not as good as PowerShell but like many of you I didn't care and just wanted to get on with my project.

Recently, VS Code updated and despite the above setting, PowerShell would run by default in terminal. Back to square one.

The one-line fix

I found another post and tried the following in my Anaconda PowerShell Prompt (Miniconda3):

conda init

which changed several paths, including some used by PowerShell. Now Python scripts running in VS Code run in PowerShell because PowerShell now recognises Conda.

Note

If you've created a virtual environment you still need to indicate this in VS Code:

View --> Command Palette... --> Python: Select Interpreter -->

~\Path\to\my\virtual\environment\python.exe
Jordan M
  • 143
  • 2
  • 7
4

Easiest way is to open anaconda shell prompt, then launch VScode from that terminal. Now VScode can see everthing from anaconda!

M. Balcilar
  • 518
  • 5
  • 8
4

As many have pointed out, you can simply launch "code" from the Anaconda Command Prompt (ACP).

The ACP is simply a shortcut to a Command Prompt (and equivalently on powershell) with some arguments. (e.g. %windir%\System32\cmd.exe "/K" C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3)

As @janh pointed out, it is annoying to launch vscode from there each time, instead, you can change the default within the VS Code setting.json file, as @zonhgyi pointed out, but what he recommended is/will be deprecated.

REF: https://code.visualstudio.com/docs/editor/integrated-terminal#_configuration

Here's what I ended up using. No need to change PATH, no need to make other changes than editing the settings.json file (which can be accessed through Ctrl+Shift+P -> settings.json)

...

"terminal.integrated.profiles.windows": {
  "Conda": {
    "path": "C:\\windows\\System32\\cmd.exe",
    "args": [
      "/K", "C:\\ProgramData\\Anaconda3\\Scripts\\activate.bat C:\\ProgramData\\Anaconda3"
    ],
    "icon": "squirrel"
  }
},
"terminal.integrated.defaultProfile.windows": "Conda", 
...
ml_chai
  • 61
  • 5
3

I am working on VS Version: 1.47.0 (user setup). I recently switched from Jupyter Notebook (still working there for my reports) to VS Code. But my codes were not running. Same problem of 'conda' is not recognized. What worked for me was:

  1. Kill Terminal and start afresh
  2. In the terminal dropdown option, selected "Select Default Shell"
  3. Changed that from powershell to CMD.
  4. Now all my preinstalled libraries and conda working perfectly fine.
Nilabh
  • 169
  • 1
  • 3
  • Correct, this does work, change to CMD shell, and will start conda AND activate your miniconda environment, after your selected your interpreter (env). No need to set 'Python: Conda Path' in your settings – Coder Dev Jul 10 '23 at 23:09
3

Open file C:\Users\YOUR-USER-NAME\AppData\Roaming\Code\User\settings.json

Insert section "Miniconda3": { }

"terminal.integrated.profiles.windows": {

    "PowerShell": {
        "source": "PowerShell",
        "icon": "terminal-powershell"
    },

    "Miniconda3": {
        "source": "PowerShell",
        "args": "-ExecutionPolicy ByPass -NoExit -Command \"& 'C:\\ProgramData\\Miniconda3\\shell\\condabin\\conda-hook.ps1' ; conda activate 'C:\\ProgramData\\Miniconda3' \"",
        "icon": "C:\\ProgramData\\Miniconda3\\Lib\\site-packages\\conda\\shell\\conda_icon.ico"
    },

    "Command Prompt": {
        "path": [
            "${env:windir}\\Sysnative\\cmd.exe",
            "${env:windir}\\System32\\cmd.exe"
        ],
        "args": [],
        "icon": "terminal-cmd"
    },

    "Git Bash": {
        "source": "Git Bash"
    }

}

No need to touch env:PATH

Btw, Miniconda3.args is copied from Anaconda Powershell Prompt (Miniconda3) -> Properties.Shortcut.Target

If you are using Anaconda instead of Miniconda, you may want to modify a few paths.

VS Code is just an editor, Python Extension keeps breaking stuff, and spreads its config files everywhere. Stopped using long time ago.

Naylinn
  • 31
  • 2
  • 2
    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". – Neits Sep 30 '21 at 06:15
  • @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) – xagg Mar 27 '22 at 05:58
3

I think this is a better way, to let conda work with powershell in vscode, without change your PATH, which is unrecommanded:

  1. win + X, open terminal as Administrator.
  2. run C:\Users\xxx\anaconda3\Scripts\conda.exe init powershell, you can use where conda in Anaconda Prompt (anaconda3) to find the path.

It works for me. May be run conda.exe init powershell in Anaconda Prompt (anaconda3) directly is also helpful, but I haven't try it.

Jannchie
  • 690
  • 6
  • 18
1

A combination of the above examples and some recent profile creation worked best for me. I didn't want to replace the existing default profile, just add a new one with the right environment settings.

Go into user settings and find a link to 'edit settings json' (there's one in the extensions section).

Add a profile in the terminal.integrated.profiles.windows object:

  "terminal.integrated.profiles.windows": {
    "PowerShell": {
      "source": "PowerShell",
      "icon": "terminal-powershell"
    },
    "Command Prompt": {
      "path": [
        "${env:windir}\\Sysnative\\cmd.exe",
        "${env:windir}\\System32\\cmd.exe"
      ],
      "args": [],
      "icon": "terminal-cmd"
    },
    "Git Bash": {
      "source": "Git Bash"
    },
    "Anaconda PS Prompt": {
      "path": "C:\\Program Files\\PowerShell\\7\\pwsh.exe",
      "args": [
        "-ExecutionPolicy",
        "ByPass",
        "-NoExit",
        "-Command",
        "& 'C:\\Users\\me\\anaconda3\\shell\\condabin\\conda-hook.ps1'",
        "; conda activate 'C:\\Users\\me\\anaconda3'"
      ]
    }
  }

Save the file and it should immediately be available in the terminal drop-down. If not, restart VS Code.

NOTE: I got my 'args' above from the Anaconda Prompt shortcut, just go to the file location and check properties (it's a .lnk file if you're using a start menu shortcut, so you need to see where that is pointing).

Astravagrant
  • 685
  • 10
  • 18
0

Firstly, you have to add 4~5 path in environment variable

enter image description here

somebody may not have .../usr/bin. it's not important.

and secondly, you have to change default profile to command prompt in vs code. Using default shell or power shell would have some problem. in my case , I can't activate conda environment which I created. it keep stop in the (base) env of anaconda. press ctrl+shift+p select terminal:select default profile and click. Then choose command prompt. when changing into command prompt, I can use activate (myenvname) to other env.

enter image description here

Majid Hajibaba
  • 3,105
  • 6
  • 23
  • 55
0
  1. Click control+shift+p

  2. Search Teminal: Select Default profile

  3. Open new terminal (python recent virtual environment)

  4. Again click control+shift+p

  5. Search Python: Select Interpreter (choose your environment or base conda environment)

  6. Open new terminal (conda virtual environment)

ouflak
  • 2,458
  • 10
  • 44
  • 49
0

This worked for me:

C:\Users\xxx\Anaconda3\Scripts C:\Users\xxx\Anaconda3 C:\Users\xxx\Anaconda3\Library\bin

Just add these three path to your Path

Oscar Rangel
  • 848
  • 1
  • 10
  • 18
0

For me my interpreter was set to a different version:

CTRL+SHIFT+P -> Select Python: Select Interpreter

Select the latest version.

  • but that I can't use that Python in the terminal. It can only click on the run button with a default interpreter. What if you have argument parsing? – Prakhar Sharma Oct 06 '22 at 15:24
0

Given that you have anaconda installed, open the "Command Prompt" terminal:

  1. /Users/NAME/anaconda3/Scripts/activate

This will activate conda prompt, and you can continue to select, create, deactivate any environment you want. Make sure to specify the path to anaconda3 (replace NAME)