Use Visual Studio Code with Miniconda on macOS

Viewed 3568

On my Mac, I installed Python using Miniconda and I have also installed various Python packages using conda. In the Mac Terminal app, my base conda environment is active and when I run Python it correctly uses the Miniconda version of Python as shown below:

python in Mac terminal

In Visual Studio Code (VSCode), I have selected the Miniconda version of Python using the Python Interpreter setting. The correct version of Python and conda environment is shown at the bottom of the window:

python interpreter

However, when I use the built-in terminal in VSCode it still uses the macOS Python even though it shows the base conda environment is active:

vscode terminal python

I'm using the Python, One Dark Pro, and Jupyter extensions in VSCode. Also, the $PATH in the VSCode terminal and in the Mac terminal app returns the following:

# VSCode terminal
base ❯ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/Library/Apple/usr/bin:/Users/gavinw/miniconda3/bin:/Users/gavinw/miniconda3/condabin
# Mac terminal app
base ❯ echo $PATH
/Users/gavinw/miniconda3/bin:/Users/gavinw/miniconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/Library/Apple/usr/bin

How can I configure the Visual Studio Code terminal to use the Miniconda version of Python?

2 Answers

After selecting the Python that comes with conda, the Python interpreter will be displayed in the lower left corner of VSCode, and then use the shortcut key Ctrl+Shift+` to open a new VSCode terminal, (or click "Terminal", "New terminal") it will automatically enter the selected environment and activate it automatically.

enter image description here

The terminal uses whatever python environment your configured shell command is set to. It is in no way linked to the environment of VS Code

However, If you attempt to run a Python file, it will open up the terminal with the full path to the specified interpreter

These details are laid out in the docs

The Python extension uses the selected environment for running Python code (using the Python: Run Python File in Terminal command), providing language services (auto-complete, syntax checking, linting, formatting, etc.) when you have a .py file open in the editor, and opening a terminal with the Terminal: Create New Integrated Terminal command. In the latter case, VS Code automatically activated the selected environment.

Also, if using code command from an activated session...

launching VS Code from a shell in which a certain Python environment is activated does not automatically activate that environment in the default Integrated Terminal.

Otherwise, launching the desktop app directly would also not be done from an activated conda environment

You may want to check the value in your settings for "python.terminal.activateEnvironment", but I believe this only applies when you actually run a .py file

Related