11

I am on MacOS Mojave and I can't figure out why visual studio code's integrated terminal has a different path variable than the normal terminal.

I have checked that VSCode opens /bin/bash (echo $SHELL yields /bin/bash).

However when executing echo $PATH in the integrated terminal it yields /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/anaconda3/bin:/anaconda3/condabin while on the normal terminal it gives /anaconda3/bin:/anaconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

They both source ~/.bash_profile which contains the following :

# added by Anaconda3 2018.12 installer
# >>> conda init >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$(CONDA_REPORT_ERRORS=false '/anaconda3/bin/conda' shell.bash hook 2> /dev/null)"
if [ $? -eq 0 ]; then
    \eval "$__conda_setup"
else
    if [ -f "/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/anaconda3/etc/profile.d/conda.sh"
        CONDA_CHANGEPS1=false conda activate base
    else
        \export PATH="/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup

Even after sourcing ~/.bash_profile in the integrated terminal, /anaconda3/bin is at the end while ~/.bash_profile prepends it which is confusing.

I think the default path must be added after bash_profile is sourced in the integrated terminal? Not sure how it works

I am hesitating to post this as an issue in the vscode repo but I just wanted to make sure it isn't my understanding that is off.

5 Answers5

17

In Visual Studio Code open Settings (JSON) from the command palete (⇧⌘P), and at the end of the file add the following

"terminal.integrated.env.osx": {
        "PATH": ""
}

Restart VSCode and you are all set.

Taken from https://github.com/Microsoft/vscode-python/issues/4434

diegouis
  • 171
9

You can uncheck Terminal › Integrated: Inherit Env option, which can be searched by terminal.integrated.inheritEnv, in Visual Studio Code Settings like the following image:

enter image description here

Referece: https://github.com/microsoft/vscode/issues/70248#issuecomment-502186149 (Now the option terminal.integrated.inheritEnv is included in the stable build.)

3

Turning off the Enable Persistent Sessions settings fixed it for me.

Enable Persistent Sessions Settings Screenshot

It seems that, when this setting is on, VSCode caches the value of PATH and never updates it.

Source for the solution is this StackOverflow answer.

2

This was driving me insane. This worked for me: "terminal.integrated.shellIntegration.enabled": false

It supposedly "determines whether or not shell integration is auto-injected to support features like enhanced command tracking and current working directory detection". With it on, it added a bad path that caused my conda to not be found.

Version: 1.89.1 (user setup) Commit: dc96b837cf6bb4af9cd736aa3af08cf8279f7685 Date: 2024-05-07T05:13:33.891Z Electron: 28.2.8 ElectronBuildId: 27744544 Chromium: 120.0.6099.291 Node.js: 18.18.2 V8: 12.0.267.19-electron.0 OS: Windows_NT x64 10.0.22631

gojira
  • 21
0

This issue still seems to persist. I fixed it by explicitly telling VSC which interpreter to use.

  1. Open the command pallet (⇧⌘P).
  2. Search for "python select interpreter".
  3. Select the version of Python that matches what you use in terminal.

If you are unsure where your normal version of Python is, open terminal and use which python to get its path. Since I'm using Anaconda too, mine is located in /opt/anaconda3/bin/.

This immediately fixed the $PATH discrepancies and all of my imports were working as expected. I also did a deep dive and cleaned up all of my various Zsh profiles but that's another topic.