Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Is macOS Visual Studio Code Using Wrong Python Interpreter?

I'm running VS Code 1.33.1 on macOS Sierra (10.12.6). When I run a simple Python program like the following, VS Code uses Python 2.7 as installed on my Mac rather than Python 3 which I installed using Homebrew.

    # show-python-version.py
    import sys
    print(sys.version)

Here's the output as displayed in the VS Code Output window:

    [Running] python -u "/Users/smith/Documents/Programming/Python/Examples/show-python-version.py" 
    2.7.15 (default, May  1 2018, 16:44:37) 
    [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)]

    [Done] exited with code=0 in 0.032 seconds

As you can see, I'm getting version 2.7.15 because it's calling "python" instead of "python3", but I don't know how to get it to use python3. Here are all the things I've done to try to fix this problem:

  1. I've ensure that none of my virtual environments that use Python 2.7 are running when I issue the "code ." command in the Examples directory.

  2. I installed Python 3 using Homebrew so I opened the Command Pallette (shift + cmd + p), typed in "Python: Select Interpreter", and confirmed that it's using my Homebrew version: current: /usr/local/bin/python3.

  3. I checked VS Code's Settings, searched for "python.pythonPath" and confirmed that it's the same as the interpreter path shown in step 2 above.

  4. I also examined the file Examples/.vscode/settings.json to confirm the interpreter path there too:

    {
            "python.pythonPath": "/usr/local/bin/python3"
    }
    
  5. I've restarted VS Code to no avail.

I have these extensions installed:

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

It's may be related to my path which looks like the following since both python and python3 are in /usr/local/bin:

    $ echo $PATH
    /usr/local/bin:/usr/bin:/bin:/usr/sbin:....

What am I doing wrong?

like image 711
Jim Avatar asked Nov 06 '25 04:11

Jim


1 Answers

It's because of the code runner extension. Add this "code-runner.executorMap.python": "python3 -u" to your settings.json and that should change the python version for when it runs code to python3 instead of python2

like image 57
Matthew Barlowe Avatar answered Nov 08 '25 00:11

Matthew Barlowe