简体   繁体   中英

VSCode change default python version

When I code on VSCode and don't specify a python version it is set to python2.7.17

I would like to set it by default to python3.6

I tried doing so by adding this at the top of my script: #./usr/bin/python3.6 But it is not working

#!/usr/bin/python3.6
from flask import Flask, render_template, request, session
from datetime import timedelta
import sys
print(sys.version)

This code outputs:

2.7.17 (default, Apr 15 2020, 17:20:14) 
[GCC 7.5.0]

I can it output the version 3.6?

My settings.json looks like this: 在此处输入图像描述

Even with a specified path the sys.version outputs python2

In VSCode's command palette choose the option

Python: Select Interpreter

And choose the interpreter you want to use. This will automatically add the setting correctly to your settings.json file in your .vscode folder for your workspace.

python列表vscode

This will take care of Python management in VSCode.

Now coming to setting the right python to be used by your os/terminal when trying to run scripts.

Since you are using linux, make sure your python is the right version thats linked

  1. Check python version on terminal - python --version
  2. execute sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1 << verfiy your paths correctly
  3. then run sudo update-alternatives --config python
  4. Select the python version
  5. Try python --version again and check

If on Windows make sure path to Python 3.8.x is set at a higher priority in your Path Environment Variable. Py3 should be above your other paths

Python 的路径

Open settings and search for Python Default Interpreter Path .

默认 python 解释器的 VS 代码设置

In your case, you wish to set it to /usr/bin/python3.6 . Though I would suggest using a virtual environment .

{  
  "python.defaultInterpreterPath": "/usr/bin/python3.6"
}  

If you are using virtual environment, to ensure that the terminal is using the associated python environment , check the second option Python > Terminal: Activate Environment :

在 VS Code 中激活 Python 环境

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

One reason your change did not persist, was because in your screenshot, you opened your workspace's settings.json . To make it default, you should edit your user's settings.json instead. This you can access with Ctrl+Shift+P , Preferences: Open Settings (JSON) . Detailed instructions can be found in the documentation "Manually specify an interpreter" , including using environment variables as the interpreter's path.

Also, "python.pythonPath" has been deprecated :

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM