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).
How do I configure VS Code to use my Conda environment?
In Visual Studio Code you can run Python code with Anaconda by using the Anaconda Prompt, updating the Visual Studio Code workspace settings to be aware of your Anaconda installation, or adding Anaconda to the Windows Path variable.
Conda environments can't be automatically activated in the VS Code Integrated Terminal if the default shell is set to PowerShell. To change the shell, see Integrated terminal - Terminal profiles. You can manually specify the path to the conda executable to use for activation (version 4.4+).
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:
This setting enables VS Code to recognize the available conda environments
. Thus, we can click on the current python interpreter and switch to others:
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
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:
cd /projects/cool_name
conda activate my_env
code .
This will launch new VScode window in your current directory and using the debugger should pick up my_env
.
On MacOS, follow these steps:
Install Python for VSCode (the most popular python extension)
Go to Workplace Settings by pressing Cmd ,
or navigating from the top menu.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With