Efficient Way to Activate Conda in VSCode

Kathryn
Analytics Vidhya
Published in
7 min readMay 27, 2020

I was struggling quite a bit while I started to use VSCode, because there is a lot of flexibility in VSCode, but it also implies that there is tons of detailed settings which allows all kinds of alternative usages. This post is going to introduce the head-to-toe process of how to use Conda environment in VSCode. The post would focus on Windows system, but the other OS users would find it useful too.

As the primary language I use is Python, I will use Python configuration in this demo, and let’s first look at two ways of running Python code in VSCode.

In VSCode you can choose to run Python file in Debugger, or Run Python file in the terminal.

Run in Terminal mode

One option to run Python is to run it in the terminal.
On the top right corner, there is a button “Run Python File in Terminal”. Once you click it, your Python script will be executed in the TERMINAL window.

When you run python in the terminal, it will adopts the configurations in “settings.json”.

To find the configuration file, use CTRL+SHIFT+P to call out the commands line panel, and search “settings”.

You will see two JSON settings: Preference: Open Workspace Settings (JSON) and Preference: Open User Settings (JSON)

The difference between these two is that the Workspace one would only config your settings to your current workspace (working directory), and the User one is like a global default fallback setting, which applies to all of your VSCode projects while there is no .vscode folder (workspace setting) found in your working directory.

Here, I use the Preference: Open Workspace Settings (JSON), because I only want to use this Python environment for this project.

Here we add three things in the configurations:

python.pythonPath”:“C:\\Users\\<your-usrname>\\Anaconda3\\envs\\<your-conda-env>\\python.exe”

python.terminal.activateEnvironment”: true

“terminal.integrated.shell.windows”: “C:\\Windows\\System32\\cmd.exe”,

If you have problem finding the conda virtual environment path, you can open a terminal with your target conda env activated, and then type: where python and it will show you the existing python.exe in your system.

python.pythonPath would specify which Python version you want to use to run Python in terminal. We simply use the one in the specific conda environment.

The second settingpython.terminal.activateEnvironment”: true, would allow the VSCode to activate the conda environment, which was detected via python.pythonPath, when a terminal was created.

The third one “terminal.integrated.shell.windows” is the default terminal console you like to use. I set the default to be cmd.exe, as the other Windows option powershell is not familiar to everyone.

After you’ve finished the setup, now you have two options to activate the conda environment for the VSCode terminal. One is to Create New Integrated Terminal (In Active Workspace) and the terminal would activate the conda environment based on the Python interpreter you specified in python.pythonPath. The other one is manually run “conda activate <env>” in the terminal.

Option 1: Create New Integrated Terminal (In Active Workspace)

  1. Ctrl+Shift+P and search “Terminal”.
  2. Choose “Terminal: Create New Integrated Terminal (In Active Workspace)
  3. In the Terminal window, you can see “conda activate <env>” command already executed, and the terminal is under your conda environment.

Option 2: Run “conda activate <env-name>” in terminal

The second way is to have manual control of switching the conda environment in the terminal. To achieve this, we need to make VSCode terminal recognize the “conda” commands.

  1. CTRL+SHIFT+P open “Preference: Open User Settings (JSON)”. We choose User settings instead of Workspace settings is because we want to set this commands globally in VSCode across different projects.
  2. Set the “python.condaPath” variable to

“python.condaPath”: “C:\\Users\\<your-user-name>\\Anaconda3\\Scripts\\conda.exe”,

You can google online how to find the conda executable path in your system, because I noticed that different Anaconda version could have different paths.

3. Now, you can start to use the ‘conda’ commands in the VSCode terminal:

conda activate <env-name>
conda install <pkg-name>
conda info --envs

Debugger mode

Another option is to use the debug mode. Debug mode allows you set breakpoints to stop the code execution at the certain lines you specified, so as to give programmers a chance to investigate what happened in the middle of the code before it returns an output.

One can press the icon on the left menu panel that looks like a triangle with a bug, and then on the top toolbar, a triangle button shows ‘Start Debugging’.

screenshot of Run with Debugging mode

Then it would call the corresponding Python interpreter that is specified in “launch.json” file. To find the file, click on the drop down button and choose the default “Python: Current File”. Then “launch.json” would pop up.

In the “launch.json file, specify your pythonPath to point to python.exe from a conda environment:

“pythonPath”: “C:\\Users\\<your-user-name>\\Anaconda3\\envs\\<your-conda-env>\\python.exe”

Note 1: Windows users need to use \\ double slashes when specifying path.
Note 2: The Anaconda3 path may differ depends on where you installed miniconda or Anaconda.

This config will automatically adopt to using the Python under the conda environment you specified here (because that python.exe would import library from the ‘Lib/site-packages’ under its own directory). And now you can start debug your Python code with all the libraries you installed under this environment. You can find the result in DEBUG CONSOLE window.

[Update]

While this works for most cases, recently I found that VSCode “internalconsole” Debugging mode does NOT preemptively active conda environment before the debugging was run. This did not affect most of my 3rd party Python library, however it fails my numpy import especially. (The same bug was reported by many on VSCode’s Github forum, and people are still reporting the same issue up to the time I am writing this post.)

One workaround more like a hacking is:
(1) Change the console type to integratedTerminal in “launch.json”.

“console”: “integratedTerminal”

(2) Add integrated shell arguments in “settings.json” to force the integrated terminal run the conda command every time when being initiated.

“terminal.integrated.shellArgs.windows”: [“/K”, “conda activate <env-name>”]

Note: You need to first setup Python.condaPath to be able to use conda command like this. See the previous section Option 2.

(3) Allow “internalConsole” to open on session starts in “launch.json”.

“internalConsoleOptions”: “openOnSessionStart”

With these three steps you can initiate your debug through “integrated terminal” which has the full control power to activate conda environment, while the “internalConsole” (a Python console) would open an window in the DEBUG CONSOLE at the same time for debugging purpose.

For example, two breakpoints are set at line 5 and 7, and thus the first print(a) from line 4 is ouput in the TERMINAL.

and when switch to DEBUG CONSOLE, you can interact with the running Python code. For example, in the bottom input area, you can use is a an active interpreter to check the current variable:

However, one thing remains really annoying is there is a latency of “conda environment activation“ after the debug environment is initiated (See the top answer in stackoverflow). So my import numpy would still fail when I ran “Start Debugging” in this environment for the first time (i.e., without a prior “Python Debug Console” already initiated). But it works after I click “continue” when the Debugging session remains active, and it works hereafter.

That’s it! Hope you enjoy this tutorial about how to work in VSCode under conda environment! If you find this post to be useful, please leave a “clap” to this post! You are also welcome to leave your thoughts, comments or feedback! Thanks for your reading!

--

--