VSCode更改默认Python版本

时间:2020-06-14 19:34:41

标签: python visual-studio-code

当我在VSCode上编码且未指定python版本时,它将设置为python2.7.17

我想将其默认设置为python3.6

我尝试通过在脚本顶部添加以下内容来做到这一点:#!/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)

此代码输出:

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

我可以输出3.6版本吗?

我的settings.json看起来像这样: enter image description here

即使具有指定路径,sys.version也会输出python2

2 个答案:

答案 0 :(得分:1)

在VSCode的命令面板中,选择选项

Python: Select Interpreter

然后选择要使用的解释器。这将自动将设置正确正确地添加到工作区的settings.json文件夹中的.vscode文件中。

python list vscode

这将处理VSCode中的Python管理。

现在尝试在运行脚本时设置正确的python,以供操作系统/终端使用。

由于您使用的是Linux,请确保您的python是链接的正确版本

  1. 在终端上检查python版本-python --version
  2. 执行sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1 <<正确验证您的路径
  3. 然后运行sudo update-alternatives --config python
  4. 选择python版本
  5. 再次尝试python --version并检查

如果在Windows上,请确保在路径环境变量中将Python 3.8.x的路径设置为更高的优先级。 Py3应该位于其他路径之上

Path for Python

答案 1 :(得分:0)

打开设置并搜索 Python Default Interpreter Path

VS code settings for default python interpreter

就您而言,您希望将其设置为 /usr/bin/python3.6。虽然我建议使用 virtual environment

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

如果您使用的是虚拟环境,to ensure that the terminal is using the associated python environment,请选中第二个选项 Python > Terminal: Activate Environment

Activate Python Environment in VS Code

settings.json 中,它被称为 "python.terminal.activateEnvironment": true

您的更改没有持续存在的一个原因是,在您的屏幕截图中,您打开了工作区 settings.json。 要将其设为默认值,您应该改为编辑您的用户 settings.json。 您可以使用 Ctrl+Shift+PPreferences: Open Settings (JSON) 访问。 详细说明可以在文档 "Manually specify an interpreter" 中找到,包括使用环境变量作为解释器的路径。

另外,"python.pythonPath" has been deprecated

<块引用>

2021.6.0(2021 年 6 月 16 日)
5. 在 pythonDeprecatePythonPath 实验中,在工作区级别添加了 python.defaultInterpreterPath 设置。 (#16485)
8.在pythonDeprecatePythonPath实验中显示python.pythonPath弃用提示。 (#16485)

<块引用>

2020.7.0(2020 年 7 月 16 日)
9. 在 Deprecate PythonPath 实验中,提示用户我们已从他们的工作区设置中删除了 pythonPath。 (#12533)

<块引用>

2020.5.0(2020 年 5 月 12 日)
6. 如果在 DeprecatePythonPath 实验中,将 python.pythonPath 设置的现有值一次性传输到新的解释器存储。 (#11052)
8. 添加了在 Deprecate PythonPath 实验中要求用户从他们的工作区设置中删除 python.pythonPath 键的提示。 (#11108)
12. 将launch.json 中用于引用设置中设置的解释器路径的字符串${config:python.pythonPath} 重命名为${config:python.interpreterPath}。 (#11446)

<块引用>

2020.4.0(2020 年 4 月 20 日)
13. 新增用户设置python.defaultInterpreterPath,用于在Deprecate PythonPath实验中设置默认解释器路径。 (#11021)