Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 900
Paige Avatar asked Jan 07 '18 08:01

Paige


People also ask

How do you change the Python version in VS Code?

To do so, open the Command Palette (Ctrl+Shift+P) and enter Preferences: Open User Settings. Then set python.defaultInterpreterPath , which is in the Python extension section of User Settings, with the appropriate interpreter.

Can you edit python in Visual Studio code?

Working with Python in Visual Studio Code, using the Microsoft Python extension, is simple, fun, and productive. The extension makes VS Code an excellent Python editor, and works on any operating system with a variety of Python interpreters.


2 Answers

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.

like image 104
Natsfan Avatar answered Sep 28 '22 07:09

Natsfan


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", ... 
like image 20
Rustam A. Avatar answered Sep 28 '22 08:09

Rustam A.