How can I change the Python version in Visual Studio Code?
Asked Answered
K

19

89

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?

Knurl answered 7/1, 2018 at 8:40 Comment(2)
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.Heehaw
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.Crusted
M
135
  1. Under the View menu select Command Palette... F1 (or press F1 key).

  2. Type Python: Select Interpreter.

  3. Choose which Python version to use by default [1].


[1] You can safely disregard the "Recommended" hint, which is usually the bare bones system one, without access to your custom packages.

Morsel answered 7/1, 2018 at 9:19 Comment(10)
@jmh please could you file an issue for this on the python extension repo. If like to get to the bottom of this and help you get this working. github.com/Microsoft/vscode-python/issues/newBreviary
@Don, it works as expected for me. I don't know what issue I'd file.Morsel
I tried this, but it didn't work. When I checked the python version in the terminal, it still said a different version than the interpreter I selected, in spite of starting a new instance of terminal.Pharyngo
I searched for hours and hours how to make my python code work on vscode, your answer took me all out, thank you so much!Xylon
This was very frustrating experience to say the least. For me, just restart my computer did resolved this nonsense issue. It was working in Python 3.7 and all of sudden it switches to python 2.7. I do use Visual Studio for PHP env as well.Lend
I have done this 3 times. It does not work. Whatever the answer is, it is not 'Python: Select Interpreter'. I have Python 3.8 selected. But code chooses version 3.7.2. I have rebooted. It still chooses 3.7.2 . So whatever solution does this it is not simply using the command pallet. I can find the right version by copying what Code says and running it on the command line. But Code, does not run that command that it says it is supposed to.Banjo
if you have Python 3.8 installed on your machine you need to have a virtual environment defined for 3.8 using the following: conda create -n yourenvironmentname python=3.8 then activate that environment. After this try running python. It should start with python 3.8 and no reason to change interpreters.Morsel
@Pharyngo a terminal is a terminal. You need to click the play or debug button in the IDE for the full path of the python interpreter to execute the source code. If you selected python 3.6 in Visual Studio Code > View > Command Palette (CTRL+SHIFT+P) > Python: Select Interpreter, the play (execute) button will begin the call with the full path of that interpreter followed by the *.py file. If you want the specific version, copy the full path of the python.exe file and add " --version" after it to get the version.Mcafee
For future reference, here's instructions on how to do this in the docs for vs code: code.visualstudio.com/docs/python/python-tutorialOrvieto
this solved my issue exactly. go nats.Oquendo
C
23

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",
...
Cowie answered 10/4, 2019 at 19:39 Comment(4)
thanks - that worked for me on my mac, where python 2.x is installed by defaultOraliaoralie
Thanks! This worked on linux where python2 is installed by default as "python".Whitener
Thanks man, I changed mine to python3.9 and now it works like a charm. You don't know the many times I changed the interpreter to py3.9 and banged my keyboard in frustration. FRIGGIN code runner. There are like 5 python installations on my pc lol.Cucullate
Thanks! I was pulling my hair out trying to figure out why the debug Run button used the correct interpreter (python3 from a virtual environment) but the regular (non-debug) Run used the first it found (python2).Anfractuosity
L
18

Several of the answers here explain good approaches, but below are my top 2 recommendations.

1) Bottom Screen Navigation (ease of access)

  • I find this the 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 easiest 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 for 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 as seen in the snippet below:

Visual Studio Code Snippet

2) 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 bring about the same menu denoted above.

If you're still having issues, there's also a VS Code Getting Started guide that walks you through setting up Virtual Environments and/or choosing different interpreters for Python that support the desired language: https://code.visualstudio.com/docs/python/python-tutorial

Happy coding!

Lucialucian answered 26/12, 2020 at 22:18 Comment(4)
This should be the definitive answer.Selfesteem
Just want to note that in my VS code, this appears on the right next to language mode and other text file related tabs.Tiliaceous
doesn't work for me (it somehow list some of my scripts using a specific version of python i.imgur.com/dnl0x2o.png , selecting 3.6 doesn't change anything, it remains in 3.9)Kele
Hey @JinSnow, want to see if following this link helps further? With various scripts installed, you may want to look into creating a virtual environment or installing different interpreters that target your desired version: code.visualstudio.com/docs/python/python-tutorialLucialucian
J
13

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.
Jiujitsu answered 2/1, 2020 at 11:29 Comment(1)
does this setting still exist? I can't see it with the Python extension installed.Gutter
S
8

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.

Souse answered 10/12, 2018 at 3:37 Comment(1)
If you add a new line (press enter to create a blank line) before your first point you will see the output formatted correctly as the list you presumably intendedParsec
K
8

VS Code's terminal using a different python interpreter than the one you've selected

By default, it doesn't know about your interpreter, and will initialize using the default .bashrc or equivalent in the OS.

I found two relevant settings from an issue in Feb 2021. Checking the second option Python > Terminal: Activate Environment enables automatic activation of virtual environment:

Activate Python Environment in VS Code

In settings.json it is called "python.terminal.activateEnvironment": true.

Update (8 Aug 2021): Today when I opened the terminal from VS Code on Windows, it automatically inserted a line of code & C:/Users/[UserName]/[venv]/Scripts/Activate.ps1 to activate the appropriate environment associated with the selected python interpreter! It appears the aforementioned settings is now the default behavior. While there are changes to Terminal behavior in the release notes of July 2021 (version 1.59), I don't see virtual environment activation being explicitly mentioned.

The new behavior is documented here, in "Environments and Terminal windows".


"python.pythonPath" has been deprecated

Therefore most previous answers are outdated. Use "python.defaultInterpreterPath" instead:

2021.6.0 (16 June 2021)
5. Added python.defaultInterpreterPath setting at workspace level when in pythonDeprecatePythonPath experiment. (#16485)
8. Show python.pythonPath deprecation prompt when in pythonDeprecatePythonPath experiment. (#16485)

2020.7.0 (16 July 2020)
9. Prompt users that we have deleted pythonPath from their workspace settings when in Deprecate PythonPath experiment. (#12533)

2020.5.0 (12 May 2020)
6. Do a one-off transfer of existing values for python.pythonPath setting to new Interpreter storage if in DeprecatePythonPath experiment. (#11052)
8. Added prompt asking users to delete python.pythonPath key from their workspace settings when in Deprecate PythonPath experiment. (#11108)
12. Rename string ${config:python.pythonPath} which is used in launch.json to refer to interpreter path set in settings, to ${config:python.interpreterPath}. (#11446)

2020.4.0 (20 April 2020)
13. Added a user setting python.defaultInterpreterPath to set up the default interpreter path when in Deprecate PythonPath experiment. (#11021)

If you wish to set a default python interpreter for all workspaces, open settings with Ctrl+Shift+P, Preferences: Open User Settings and search for Python: Default Interpreter Path. Otherwise, if you want to set it for only the current workspace, use Preferences: Open Workspace Settings instead.

VS code settings for default python interpreter

In your case, you wish to set it to ${workspaceFolder}/env/bin/python3.6. If you edit settings.json directly instead of using the GUI:

{  
  "python.defaultInterpreterPath": "${workspaceFolder}/env/bin/python3.6"
}  

Detailed instructions can be found in the documentation "Manually specify an interpreter", including using environment variables as the interpreter's path.

Killjoy answered 3/8, 2021 at 11:55 Comment(0)
D
7

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 🙄.

Dagnydago answered 20/1, 2021 at 20:21 Comment(0)
D
4

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!!!

Dordogne answered 5/2, 2019 at 14:5 Comment(0)
P
4

Updating @Isabella answer, using current VSCode 1.65.1 and current python launcher, you can use py -version, for example py -3.8.

Thus, you can make a folder .vscode containing a single file name called settings.json containing

{
  "python.defaultInterpreterPath": "py -3.8",
}
Pantechnicon answered 14/3, 2022 at 5:28 Comment(0)
V
3

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.

Voltmer answered 19/9, 2020 at 8:50 Comment(0)
T
3

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.

Tal answered 8/3, 2021 at 20:5 Comment(0)
G
2

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

Goglet answered 8/1, 2020 at 0:40 Comment(2)
what is that env variable?Lend
@Nguaial Presumably PATH, which you should not 'delete', but the offending element (here: C:\Users\Mike.windows-build-tools\python27) needs to be removed from the list.Sublunar
P
2

Windows: Use py -3.6 --version or to create virtual environment py -3.6 -m venv venv36

See demo

Piceous answered 14/2, 2022 at 8:55 Comment(0)
G
2

For Windows Users:

I was recently faced with a similar situation where my newly downloaded python version would not show in the terminal even when I had selected the correct interpreter using 'Python: Select Interpreter'.

Here are the steps I followed that finally made it work:

  1. Go to 'Edit the system variables' in settings.
  2. Under the 'Advanced' tab, click the 'Environment Variables' button.
  3. In the top half of the new window that opens, you will find 'User Variables'.
  4. Find the path of your old python version in there.
  5. Replace it with the path of your new python version.
  6. Now check your python version in the VScode terminal again.
Galop answered 12/10, 2022 at 14:56 Comment(0)
J
1

In Vscode you can go to preferences > settings, then on the right menu click on the first icon which is JSON. Look for "python.pythonPath" and "python.defaultInterpreterPath" and change the path. To find python3 path, open terminal and execute python3 dummyname. Actually you'll face an error but the point is it will show you the path!

Justitia answered 25/7, 2021 at 14:27 Comment(0)
L
1

One thing that can also be missed is the shell profile you are using in your terminal where you see the wanted python version vs the profile in your VS Code. please see the selected terminal profile that was given me incorrect python version

  1. Hold cmd + p and write >Terminal: Select default profile
  2. Select bash or whatever you used to install the python3 version in the terminal at the global level.
  3. Closs the terminal and VS Code and then open it again, this time VS Code will pick the correct version

No you will see the correct python version

Lansing answered 14/9, 2021 at 18:16 Comment(0)
B
1

enter image description here

from the Lower left corner, click on the python, then vscode will navigate you to all python version that installed in your system, and select the right one for you.

Behlke answered 27/10, 2021 at 5:45 Comment(0)
A
1

Installing Anaconda, then selecting it as the interpreter is an easy way to set Python to the latest version and get the extensions that will make your Jupyter Notebook working.

Adaiha answered 18/5, 2022 at 10:40 Comment(0)
J
1

None of above solutions work for me. I updated my python3 version manually by code

How to update python version in Ubuntu
Scroll to the end.
Follow Use Python 3.12 as default Python3 section steps

Jacobi answered 11/12, 2023 at 16:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.