3

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?

wigging
  • 8,492
  • 12
  • 75
  • 117
  • I had a similar issue and ended up making a quick-and-dirty workaround: https://stackoverflow.com/a/68073618/1584906 – Wolfy Jun 21 '21 at 19:24

2 Answers2

0

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

Jill Cheng
  • 9,179
  • 1
  • 20
  • 25
  • This does not work for me. Please see my updated question. – wigging Dec 16 '20 at 14:05
  • @wigging -Have you tried to reopen VSCode? I have encountered an inconsistency between the terminal and the Python interpreter, and the problem was resolved after reopening. – Jill Cheng Dec 17 '20 at 05:35
  • Yes, I tried to close then reopen VSCode and it did not fix anything. I also tried to reinstall the app and it still didn't fix anything. – wigging Dec 17 '20 at 16:03
  • @wigging -Are you using _cmd_ terminal or _Powershell_ terminal in VSCode? and have you set the relevant Python path content in the "settings.json" file? – Jill Cheng Dec 18 '20 at 08:29
  • I'm using the built-in terminal on the Mac. It's just called Terminal. – wigging Dec 18 '20 at 14:16
0

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

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Sure but why is VSCode still using the macOS version of Python even after I tell it to use the conda version of Python? – wigging Dec 16 '20 at 14:13
  • You're telling vscode, not the bash/zsh process. Like I said, they're independent – OneCricketeer Dec 16 '20 at 14:15
  • If you're referring to the `python` command output, that would depend on the `$PATH` that gets set in that terminal session – OneCricketeer Dec 16 '20 at 14:17
  • The path is set by conda in my `.zshrc` file. – wigging Dec 16 '20 at 14:17
  • And vscode is configured to use zsh as your shell process? I know mine used to be set to bash – OneCricketeer Dec 16 '20 at 14:19
  • Yes, VSCode is using `/bin/zsh` as the default shell process. – wigging Dec 16 '20 at 14:22
  • Can you include the PATH output in your question for both terminals? – OneCricketeer Dec 16 '20 at 14:25
  • The fact that `/Users/gavinw/miniconda3/bin` appears after /usr/bin and /bin would indicate that the `base` environment is in fact _not_ activated in your vscode terminal. And the first python command picked is the system one – OneCricketeer Dec 16 '20 at 14:51
  • So how do I fix it? – wigging Dec 16 '20 at 14:56
  • You could rearrange the path assignment in the zshrc to put miniconda bin path first. Or you could just use python3 command at all times, while keeping the system python executable alone – OneCricketeer Dec 16 '20 at 14:59
  • 1
    Sorry, I updated my question with the correct PATH output. It is indeed different between the two terminals. – wigging Dec 16 '20 at 15:06
  • Hard to say why `base ❯` appears in the vscode terminal (could be your zsh theme PS1 variable is messed up), but conda envs aren't actived by default on new terminal sessions unless you explicitly set that up in your zshrc – OneCricketeer Dec 16 '20 at 15:17