3

I am using Microsoft Visual Studio Code 1.30.2 on macOS Mojave 10.14.3. In the settings of VS Code, I have required it to use the external Terminal.app application on macOS. I also have an Anaconda distribution of Python installed.

Note that the PATH variable here reads:

~ » $PATH
bash: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/anaconda3/bin:/anaconda3/condabin: No such file or directory

VS Code

The order above is different from what I get if I fire up the regular Terminal app and get the PATH.

~ » $PATH
-bash: /anaconda3/bin:/anaconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin: No such file or directory

Terminal.app

What this effectively does is switch the versions of Python when I type in python in the two cases. In the regular terminal, I get Python 3 right away, whereas in VS Code, I am faced with Python 2.

How do I change the PATH in VS Code so that whenever I type the words python, I am always given Python 3 instead of 2?


P.S. Please note that while editing Python code in VS Code, I can always select which interpreter I want to use, but that does not change the situation with the terminal underneath.

9
  • Why don't you pick python in vscode instead of launching it through a shell? You can specify its path in the configuration. Commented Feb 6, 2019 at 15:41
  • 1
    @TheIncorrigible1 I don't understand what you mean. For me, it is convenient to execute the code I write in vscode right there in a shell inside the app instead of launching the terminal app separately and running the code externally. Commented Feb 6, 2019 at 15:44
  • In a similar case, when I had python2 and python3, I renamed the "python.exe" of python2 to "python2.exe" so that no conflicts ocurred in terminal or any other program. It works fine in vscode as well. When I type python, I get python3 and on typing python2, i get python2. Maybe this can resolve your issue. Commented Feb 6, 2019 at 15:51
  • @sk_353 Thanks for the suggestion. However, I think that won't work in my situation. The thing is when I type python in vscode, it first looks in the /usr/bin/ directory which contains a distribution of Python 2 that is used by macOS internally; whereas when I type python in the external terminal app, it first looks for python in /Anaconda3/bin/ which contains a distribution of python 3. The order of where to look is defined in a variable called PATH. Somehow vscode uses a different PATH and messes up the order. I want to know where its definition is located, so that I can fix it. Commented Feb 6, 2019 at 16:00
  • @sk_353 Renaming the system distribution of python in /usr/bin/ could mess things up with macOS. Commented Feb 6, 2019 at 16:03

2 Answers 2

1

I had the same problem, and I have a workaround solution.

I just add one line export PATH="/Users/username/anaconda3/bin:$PATH" into .bash_profile or .zshrc

The orifinal $PATH in vscode is /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/username/anaconda3/bin:/Users/username/anaconda3/condabin

And then my $PATH in vscode become /Users/username/anaconda3/envs/py36/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/username/anaconda3/bin:/Users/username/anaconda3/condabin

Please give it a try. I wish the workaround might help. Thanks :)

Sign up to request clarification or add additional context in comments.

Comments

0

It sounds like your Workspace setting python.pythonPath is set explicitly to the /usr/bin. Set that to match your preferred $PATH and you should be good to go.

Go to Preferences in VS Code, search for python.pythonPath, switch to Workspace, and set as desired.

6 Comments

Thanks. I tried this solution immediately after commenting. However, it still does not fix the problem.
Hunh. And you haven't created any .venv or other virtual environments? Maybe with conda? Can you check your Preferences for python.venvPath? How about python.envFile? Is there a .env in your Workspace folder? (${workspaceFolder}/.env) Have you tried searching your Preferences explicitly for /usr/bin in both User and Workspace?
There are no other conda environments. The python.venvPath setting is empty. I am not sure what a Workspace folder is.
Just a thought, but did you try this above answer, then exit VS Code and restart it? It may be (is likely) that the terminal is not sourcing the setting upon change.
No, it does not help restarting. The PATH is still the same.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.