30

The goal is to be able to use my environment setup from Conda/Anaconda within the visual studio code debugger. The default pythonpath configuration does not produce this effect - rather it goes to the system default python path (what you get when you type 'python' in a fresh shell).

enter image description here

How do I configure VS Code to use my Conda environment?

Andrew Schreiber
  • 14,344
  • 6
  • 46
  • 53

5 Answers5

21

I found a better solution, based on the previous ones:

Go to Workplace Settings. Override the parameter python.venvPath with the Path to folder with a list of Virtual Environments. For the case of anaconda:

User setting

This setting enables VS Code to recognize the available conda environments. Thus, we can click on the current python interpreter and switch to others:

active interpreter enter image description here

  • 1
    This is an excellent solution! Could you maybe edit your response and upload / embed the screenshots? – hoechenberger Mar 09 '18 at 11:51
  • I am able to find the environments but then the names seem to be messed up (or not match the normal terminal vs vscode's terminal). Did you ever see that bug/behaviour? https://stackoverflow.com/questions/61466629/how-to-have-conda-commands-match-behaviour-in-vscodes-integrated-terminal-and-n – Charlie Parker Apr 27 '20 at 19:15
  • 3
    Please explain what u mean by `Workplace Settings` – Elia Weiss Jan 15 '23 at 17:11
11

When starting Visual Studio Code from an activated Conda environment, I was able to use the environment variable to specify the path (MacOS):

{
    "python.pythonPath": "~/anaconda/envs/${env.CONDA_DEFAULT_ENV}/bin/python"
}

Go to Workplace Settings by pressing Cmd , or navigating from the top menu, then add this to settings.json

Varun Chatterji
  • 5,059
  • 1
  • 23
  • 24
  • I also works for Windows! You have to just change the path to correct one. – pkowalczyk Jul 13 '17 at 21:44
  • 1
    My [edit](https://stackoverflow.com/review/suggested-edits/16706445) was rejected, but I think you have a typo in path, it should be `env:CONDA_DEFAULT_ENV` according to the [doc](https://code.visualstudio.com/docs/editor/debugging#_variable-substitution) – pkowalczyk Jul 13 '17 at 23:28
9

For anyone looking for recent info on this, I recently came across this error where I had a conda env selected as the interpreter but the debugger was being launched with the base environment. This caused issues as the two environments had different versions of pyspark and caused the following error: Python in worker has different version 3.8 than that in driver 3.9

Then I came across this issue, and while it doesn't provide a fix, it gives a workaround as follows:

  • Close VScode (if running)
  • Open a terminal
  • (optional) navigate to the folder you want e.g. cd /projects/cool_name
  • Activate conda environment conda activate my_env
  • Launch VScode from the current terminal code .

This will launch new VScode window in your current directory and using the debugger should pick up my_env.

Carlos Amaral
  • 116
  • 1
  • 4
1

On MacOS, follow these steps:

  1. Install Python for VSCode (the most popular python extension)

  2. Go to Workplace Settings by pressing Cmd , or navigating from the top menu.

enter image description here

  1. Add the path of your specific conda environment, like seen above, to your settings.json file. You can find the paths of your conda environments by typing conda env list in terminal.

  2. Save and restart VS Code

Now the debugger will automatically use that environment! As far as I know, you must do this individually for each project.

For other ways of configuring your python path, here is a useful post by the extension author: https://github.com/DonJayamanne/pythonVSCode/wiki/Python-Path-and-Version

Andrew Schreiber
  • 14,344
  • 6
  • 46
  • 53
0

Hi for anyone still looking for solution on this, mine work when I open my VS Code from Anaconda Navigator.

I use M1 Mac Monterey 12.4 and followed these steps:

  1. Open Anaconda Navigator

  2. Select the environment here on Applications on _[your environment's name]_(picture)

  3. Launch VSCode

  4. Run the Debug again

Hope that helps!

Vegg64
  • 1
  • 1