简体   繁体   English

VSCode更改默认python版本

[英]VSCode change default python version

When I code on VSCode and don't specify a python version it is set to python2.7.17当我在 VSCode 上编码并且未指定 python 版本时,它设置为 python2.7.17

I would like to set it by default to python3.6我想默认设置为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但它不起作用

#!/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?我可以output 3.6版吗?

My settings.json looks like this:我的 settings.json 看起来像这样: 在此处输入图像描述

Even with a specified path the sys.version outputs python2即使使用指定的路径,sys.version 也会输出 python2

In VSCode's command palette choose the option在 VSCode 的命令面板中选择选项

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.这将自动将设置正确添加到您工作区的.vscode文件夹中的settings.json文件中。

python列表vscode

This will take care of Python management in VSCode.这将负责 VSCode 中的 Python 管理。

Now coming to setting the right python to be used by your os/terminal when trying to run scripts.现在开始设置正确的 python 以供您的操作系统/终端在尝试运行脚本时使用。

Since you are using linux, make sure your python is the right version thats linked由于您使用的是 linux,请确保您的 python 是链接的正确版本

  1. Check python version on terminal - python --version检查终端上的 python 版本 - python --version
  2. execute sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1 << verfiy your paths correctly执行sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1 << 正确验证您的路径
  3. then run sudo update-alternatives --config python然后运行sudo update-alternatives --config python
  4. Select the python version Select python版本
  5. Try python --version again and check再次尝试python --version并检查

If on Windows make sure path to Python 3.8.x is set at a higher priority in your Path Environment Variable.如果在 Windows 上,请确保在路径环境变量中将 Python 3.8.x 的路径设置为更高的优先级。 Py3 should be above your other paths Py3 应该在你的其他路径之上

Python 的路径

Open settings and search for Python Default Interpreter Path .打开设置并搜索Python Default Interpreter Path

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

In your case, you wish to set it to /usr/bin/python3.6 .在您的情况下,您希望将其设置为/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 :如果您使用的是虚拟环境, 要确保终端使用的是关联的 python 环境,请选中第二个选项Python > Terminal: Activate Environment

在 VS Code 中激活 Python 环境

In settings.json it is called "python.terminal.activateEnvironment": true .settings.json它被称为"python.terminal.activateEnvironment": true

One reason your change did not persist, was because in your screenshot, you opened your workspace's settings.json .您的更改没有持续存在的一个原因是,在您的屏幕截图中,您打开了工作区的settings.json To make it default, you should edit your user's settings.json instead.要使其成为默认值,您应该编辑用户的settings.json This you can access with Ctrl+Shift+P , Preferences: Open Settings (JSON) .您可以使用Ctrl+Shift+PPreferences: 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 :此外, "python.pythonPath" 已被弃用

2021.6.0 (16 June 2021) 2021.6.0(2021 年 6 月 16 日)
5. Added python.defaultInterpreterPath setting at workspace level when in pythonDeprecatePythonPath experiment. 5.在pythonDeprecatePythonPath实验时,在工作区级别添加了python.defaultInterpreterPath设置。 (#16485) (#16485)
8. Show python.pythonPath deprecation prompt when in pythonDeprecatePythonPath experiment. 8.在pythonDeprecatePythonPath实验时显示python.pythonPath弃用提示。 (#16485) (#16485)

2020.7.0 (16 July 2020) 2020.7.0(2020 年 7 月 16 日)
9. Prompt users that we have deleted pythonPath from their workspace settings when in Deprecate PythonPath experiment. 9. 在 Deprecate PythonPath 实验中,提示用户我们已从他们的工作区设置中删除了 pythonPath。 (#12533) (#12533)

2020.5.0 (12 May 2020) 2020.5.0(2020 年 5 月 12 日)
6. Do a one-off transfer of existing values for python.pythonPath setting to new Interpreter storage if in DeprecatePythonPath experiment. 6. 如果在 DeprecatePythonPath 实验中,将 python.pythonPath 设置的现有值一次性传输到新的解释器存储。 (#11052) (#11052)
8. Added prompt asking users to delete python.pythonPath key from their workspace settings when in Deprecate PythonPath experiment. 8. 在Deprecate PythonPath 实验中,增加了提示用户从他们的工作区设置中删除python.pythonPath 键。 (#11108) (#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}. 12. 将launch.json中使用的字符串${config:python.pythonPath}重命名为${config:python.interpreterPath}。 (#11446) (#11446)

2020.4.0 (20 April 2020) 2020.4.0(2020 年 4 月 20 日)
13. Added a user setting python.defaultInterpreterPath to set up the default interpreter path when in Deprecate PythonPath experiment. 13. 新增用户设置 python.defaultInterpreterPath 设置 Deprecate PythonPath 实验时的默认解释器路径。 (#11021) (#11021)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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