30
votes

These are my settings:

  1. User Settings

    {
        "atomKeymap.promptV3Features": true,
        "editor.multiCursorModifier": "ctrlCmd",
        "editor.formatOnPaste": true,
        "python.pythonPath": "python3",
        "command": "python3",
    }
    
  2. Workspace Settings

    {
        "python.pythonPath": "${workspaceFolder}/env/bin/python3.6",
        "git.ignoreLimitWarning": true
    }
    
  3. tasks.json

    {
        // See https://go.microsoft.com/fwlink/?LinkId=733558
        // for the documentation about the tasks.json format
        "version": "0.1.0",
        "command": "python3",
        "isShellCommand": true,
        "args": ["${file}"],
        "showOutput": "always"
    }
    

If I use the debug console, the version and path is right: debug

But the output always defaults to "python2.7", no matter what I do. output

How can I fix this?

10
What does the lower-left corner say your interpreter is set to? If that doesn't point to the interpreter you are expecting then click on it and change it. If it does then please file an issue. - Brett Cannon
For myself on OSx my default interpreter in VS Code was zsh and defaulted to 2.7. After changing interpreter to 3.6 & opening bash in VS Code all was well. - kevin_theinfinityfund

10 Answers

35
votes

Under the view menu select 'show command pallet'. One of the commands you can then select from the command palette is 'Python: Select Interpreter'. Selecting this option will allow you to choose which version of python to use.

15
votes

UPD. First, read the update #1 part in the bottom

(Try update#1 first) Looking at your screenshots I see you are using Code Runner extension. And I guess that is the way you are launching your programs. I don't know how it works internally, but adding this line into Code-Runner extension setting.json file fixed it:

"code-runner.executorMap.python": {...
"python": "python3 -u",
...}

Found it in Code-Runner GitHub repository: https://github.com/formulahendry/vscode-code-runner/issues/366

If you type "python --help", you'll see "-u" flag stands for "unbuffered binary stdout and stderr..." - don't know why it matters here.

Update #1. This became not so convenient further - I started using python's virual environments and the solution above couldn't launch these environments, cause python3 (symlink) is always linking to the same python environment.

The solution here is to use Code-Runner's supported customized parameters, so you should change "python" line in it's settings.json to:

...
"python": "$pythonPath $fullFileName",
...
11
votes

Tot's answer is what worked for me on windows 10, with a few modifications.

  1. File -> Preferences -> Settings
  2. Type in "python.pythonPath" in the search bar.
  3. Change it to what you normally run python with from the command line. If you have your Path environment variable set, that's just python. If not, it's likely the full path to the executable.
7
votes

This solution is for Mac and Linux:

To change your Python version from 2.7 to 3 do this:

  1. In Vscode click on file > preferences > settings.

  2. On the right side click on the ... (the three dots) and select (open settings.json)

  3. In the search bar type code-runner.executorMap.

  4. You can only change the settings on the right side.

  5. After the last setting type a comma then "code-runner.executorMap" and hit enter, this will copy all the settings from the default file.

  6. Look for "python" and change the command next to it to "python3".

  7. Save the changes and you should be good to go.

3
votes

Late answer really, if you find difficult to set the python version in VsCode,

If the interpreter didn't show the envname/bin/python or any desired path you want, then go to

  1. VSCODE main page -->file-->preference-->settings

  2. select the ... on the right corner side. You'll see USER SETTINGS, WORKSPACE SETTINGS, YOURAPP_NAME_SETTINGS. click on the your_app_name.

  3. "python.pythonPath": "/home/Jhon/AllWorksUbuntu/Projects/VX-350/envname/bin/python"

Play on the above to set the correct path. You're good to go!!!

2
votes

Just a preface: VS code was working fine (Using Python 3.x) and seemingly out of the blue it started using Python 2.7. The input() function would not convert the input to a string and that's when I realized what was happening. Typing Python in the terminal window showed 2.7 was running.

For me....

Even though "python.pythonPath" was pointing to a seemingly correct location (C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64), one of my environment variables was pointing to C:\Users\Mike.windows-build-tools\python27.

I deleted the environment variable and reinstalled Python 3.8 from the Microsoft Store and it immediately installed. I got a message from VS Code (it was running) that 3.8 got installed. I clicked Terminal/New Terminal, typed Python and it showed version 3.8. Typed 'Python xxx.py' and the code started working as it had been.

enter image description here

2
votes

In my case, I checked the python version using

python --version

It showed python 2.x even though my interpreter path was 3.x. So uninstalled python 2.x from my computer through control panel. Then it worked fine for me.

2
votes

Several of the answers here explain some good options but below are my top 2 recommendations.

  1. Bottom Screen Navigation (ease of access)

This is my quickest approach, however, it isn't always available for first-time users. If you're already using Python in VS Code, this is usually the simplest way to reach the Python: Select Interpreter menu. On the bottom left of your screen, look for "Python X.X.X". This is the currently detected/configured version of Python in your project, and clicking it brings you to the interpreter menu to change the Python version you're using. At the time of writing, I was using Python 3.9.1 in the snippet below:

Visual Studio Code Snippet

  1. Command Palette

As @jmh denoted in his answer, you can also use the 'View' tab to navigate to the Command Palette. In the Command Palette, search for Python: Select Interpreter to then bring about the same menu denoted above.

Happy coding!

2
votes

In VSCode there are two paths of python:

  1. Path that is used when you the python code using green play button up in the top right corner. This path can be set under CTRL+SHIFT+P Python: Select Interpreter.

ExecutePythonCode

  1. Path that is used when you type "python" in the terminal, and this is in "Environment Variables" in Windows 10 (Similar locations under Linux and Mac). In Windows 10 you can choose to have several Python versions, usually under C:\Users\YourName\AppData\Local\Programs\Python\Python##. Just make sure you change Environment variables C:\Users\YourName\AppData\Local\Programs\Python\Python## and C:\Users\YourName\AppData\Local\Programs\Python\Python##\Scripts accordingly. This will also affect which pip you use, i.e. a pip that belongs to Python 3.8, or a pip that belongs to Python 3.9. Terminal in VSCode in general pertains to your default terminal I think. So in Windows 10 when you type "python" in CMD Line, it should be the same version as VSCode terminal.

For sanity purposes you should make sure that both "Python: Select Interpreter" and the system environment variables point to the same version of Python.

Bonus goodie in Windows 10. If you don't have environment variable setup, and you type 'python' in VSCode terminal, it'll point to C:\Users\YourName\AppData\Local\Microsoft\WindowsApps\python.exe, which just opens up python link in Windows AppStore 🙄.

2
votes

Worked for me (linux user);

Assuming that you have other python versions installed in your system:

  1. Kill the old terminal
  2. Open a new terminal
  3. In the new terminal instead of write "python" to select the interpreter write "python3" or "python3.8"

Looks like put only 'python' will always bring python 2.