4

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?

6
  • What happens if you run it in a terminal window through vs code? Commented Apr 11, 2019 at 20:08
  • How is that done? "code --help" doesn't show a way to run a Python module through vscode via the command line. Commented Apr 11, 2019 at 20:31
  • right click file editor window and select Run Python File in Terminal Commented Apr 11, 2019 at 20:34
  • I see. Yes, when I do that my short program uses python3 like I want. Then why is it that if I hit ctrl + option + n, it uses python? Can this be fixed? Commented Apr 11, 2019 at 20:45
  • These keystrokes do nothing for me on my default VS code do you have them configured as a shortcut? Commented Apr 11, 2019 at 20:49

1 Answer 1

10

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

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

3 Comments

That was it! I never would have thought of that. Thanks!
I Get Unknown configuration setting for this so it doens't work
@iFunction if you don't have the code-runner extenstion the configuration setting won't exist

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.