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:

esbenp.prettier-vscode@1.8.1
formulahendry.code-runner@0.9.8
fosshaas.fontsize-shortcuts@1.5.0
ms-python.python@2019.3.6558
tht13.python@0.2.3
vmsynkov.colonize@2.2.2
vscodevim.vim@1.4.0

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?

Jim
  • 13,430
  • 26
  • 104
  • 155
  • What happens if you run it in a terminal window through vs code? – Matthew Barlowe Apr 11 '19 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. – Jim Apr 11 '19 at 20:31
  • right click file editor window and select `Run Python File in Terminal` – Matthew Barlowe Apr 11 '19 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? – Jim Apr 11 '19 at 20:45
  • These keystrokes do nothing for me on my default VS code do you have them configured as a shortcut? – Matthew Barlowe Apr 11 '19 at 20:49
  • When I go to Code > Preferences > Keyboard shortcuts and search for "Run Code" that's what it is for me. Also, if I hover my mouse over the "Run Code" button in the upper right part of the top menu bar, the same shortcut shows up there. Perhaps yours is set to something different? I actually don't like that keyboard shortcut and was thinking of changing it so I can't imagine I set Run Code to that one deliberately. – Jim Apr 11 '19 at 20:56

1 Answers1

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

Aristide
  • 3,606
  • 2
  • 30
  • 50
Matthew Barlowe
  • 2,229
  • 1
  • 14
  • 24