19

I am using Python with VSCode, and if I use Cmd+Shift+P and type Run Code, it runs the code with Python2 even though I have specified it to use Python3. I have read this tutorial: How to force VSCode to use Python 3 instead of Python 2? but I do not understand how to change it. (and I can't comment because I don't have enough reputation yay) Could anyone help? Thanks!

Maybe it could be related to the fact that the mini terminal at the bottom runs "python -u " instead of "python3 -u "? Does anyone know how to change that?

3
  • Maybe you need to set python.pythonPath to the path of the folder with python3 in it. See the VSCode Settings Reference for Python. Commented Jan 22, 2019 at 1:42
  • 1
    I have tried that, but it still runs Python 2. Commented Jan 22, 2019 at 12:29
  • Possibly relevant: Notice what the author shows as the contents of their config file in the question How to add a command line option to Python execution in vscode?. Apparently that's where you can specify the path to the python3 interpreter. Commented Jan 22, 2019 at 14:17

5 Answers 5

22

Please note the command Run Code is not provided by the Python Extension for VS Code, instead it is provided by the extension Code Runner. They are two separate extensions. You'll need to configure that extension to point to the Python Interpter you have chosen. I.e. as per their docs on the home page you have to update the settings:

{
    "code-runner.executorMap": {
        "python": "<fully qualified path>",
    }
}

Or you have another solution, that's to use the command Python: Run Python file in Terminal. This does not rely on the Code Runner extension and is part of the Python Extension hence using the interpreter you have selected.

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

3 Comments

How do you open the settings as a JSON file please, I can only get to the settings and there doesn't seem to be a way to make terminal use python 3 in there
Such an extension should be able to check which python version is in use and use that to run the file in question!
Took a while to find my way to this answer. After specifying Python 3 "everywhere", putting this into settings.json was THE way to get Run Code (CTRL+ALT+N) to recognize that I was writing Python 3 code...
8

As @Don mentioned in his answer the Run Code command is provided by the Code Runner extension not by the Python one.
Anyway, You can simply add the following to your settings.json file:

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

P.S: You need to reload your vs code after doing this so simply hit Ctrl + Shift + P and run Reload Window.

Comments

7

Try changing the selected Python environment.

select-python-interpreter

This can be accessed by

  1. Clicking on the Python label at the lower-left of the window
  2. Doing Cmd+Shift+P (Mac) OR Ctrl+Shift+P (Windows/Linux)
  3. Selecting Select Interpreter

Select Python3 instead of Python2 from the dropdown.

More info here:
https://code.visualstudio.com/docs/python/environments#_select-and-activate-an-environment

An "environment" in Python is the context in which a Python program runs.

Selecting an interpreter from the list adds an entry for python.pythonPath with the path to the interpreter inside your Workspace Settings. Because the path is part of the workspace settings, the same environment should already be selected whenever you open that workspace.

The Python extension uses the selected environment for running Python code (using the Python: Run Python File in Terminal command), providing language services (auto-complete, syntax checking, linting, formatting, etc.) when you have a .py file open in the editor, and opening a terminal with the Terminal: Create New Integrated Terminal command. In the latter case, VS Code automatically activated the selected environment.

1 Comment

Hmm, I have tried that, but it still doesn't work? Thanks!
0

I fixed this problem by making changes in the settings.json file.

Code Runner was using python -u instead of python3 -u.

To fix this, open the command palette (press Cmd+Shift+P for Mac or Ctrl+Shift+P for Windows) and then type "Open User Settings(JSON)" and then add the following:

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

enter image description here

Comments

-2

Did you check your system enviroment variables? Maybe Pyhton 2 is in your PATH instear of Python 3. You should consider uninstalling Python 2 from your PC if It's not going to be missed... I was having a similar trouble working with Machine Learn and it solved for me.

2 Comments

Hmm, but I read a few other answers on stack overflow saying deleting an Apple provided version of Python may break the entire OS, so I don't really want to do that.
Don't remove (or even considering removing) the system Python. Mac and Linux-like OS'es use it for system-related operations.

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.