Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode running Python 2 instead of 3

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?

like image 733
ash15khng Avatar asked Jan 22 '19 01:01

ash15khng


3 Answers

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.

like image 184
Gino Mempin Avatar answered Oct 19 '22 09:10

Gino Mempin


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.

like image 25
Ali Ataf Avatar answered Oct 19 '22 10:10

Ali Ataf


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.

like image 26
Don Avatar answered Oct 19 '22 08:10

Don