如何在Visual Studio Code中更改python版本?

Pai*_*ige 12 python visual-studio-code

有一个我无法解决的问题。我已经设置好了:

1.用户设置

{
"atomKeymap.promptV3Features": true,
"editor.multiCursorModifier": "ctrlCmd",
"editor.formatOnPaste": true,
"python.pythonPath": "python3",
"command": "python3",
Run Code Online (Sandbox Code Playgroud)

}

2.工作区设置

{
"python.pythonPath": "${workspaceFolder}/env/bin/python3.6",
"git.ignoreLimitWarning": true
Run Code Online (Sandbox Code Playgroud)

}

3.tasks.jason文件

{
// 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"
Run Code Online (Sandbox Code Playgroud)

}

如果我使用调试控制台并且版本和路径正确,请 调试

但是无论我做什么,我的输出始终默认为“ python2.7”。我该如何改善? 输出

jmh*_*jmh 15

在视图菜单下,选择“显示命令托盘”。然后可以从命令选项板中选择的命令之一是“ Python:Select Interpreter”。选择此选项将允许您选择要使用的python版本。

  • 我尝试过这个,但没有成功。当我在终端中检查 python 版本时,尽管启动了一个新的终端实例,但它仍然显示与我选择的解释器不同的版本。 (23认同)
  • 我已经这样做过3次了。这是行不通的。无论答案是什么,它都不是“Python:选择解释器”。我选择了Python 3.8。但代码选择版本3.7.2。我已经重新启动了。它仍然选择 3.7.2 。因此,无论解决方案如何做到这一点,它都不仅仅是使用命令托盘。我可以通过复制代码并在命令行上运行它来找到正确的版本。但是代码并没有运行它所说的命令。 (5认同)
  • 我花了好几个小时搜索如何让我的 python 代码在 vscode 上运行,你的回答让我全力以赴,非常感谢! (2认同)

Chi*_*hus 15

这里的几个答案解释了好的方法,但以下是我最重要的两个建议。

1) 底部屏幕导航(易于访问)

  • 我发现这是最快的方法;但是,它并不总是可供首次用户使用。如果您已经在 VS Code 中使用 Python,这通常是访问Python 的最简单方法:选择解释器菜单。在屏幕左下角,查找“Python XXX ”。这是当前为您的项目检测到/配置的 Python 版本,单击它会将您带到解释器菜单以更改您正在使用的 Python 版本。在撰写本文时,我使用的是 Python 3.9.1,如下面的代码片段所示:

Visual Studio 代码片段

2) 命令面板

  • 正如@jmh在他的回答中指出的那样,您还可以使用“查看”选项卡导航到“命令面板”。在命令面板中,搜索Python:选择解释器以打开与上述相同的菜单。

如果您仍然遇到问题,还有一个 VS Code 入门指南,可引导您设置虚拟环境和/或为支持所需语言的 Python 选择不同的解释器: https: //code.visualstudio.com/docs/ python/python-教程

快乐编码!

  • 对我不起作用(它以某种方式列出了使用特定版本的 python https://i.imgur.com/dnl0x2o.png 的一些脚本,选择 3.6 不会改变任何内容,它仍然保留在 3.9 中) (2认同)

小智 11

Tot 的答案是在 Windows 10 上对我有用的方法,经过一些修改。

  1. 文件 -> 首选项 -> 设置
  2. 在搜索栏中输入“python.pythonPath”。
  3. 将其更改为您通常从命令行运行 python 的内容。如果您设置了 Path 环境变量,那只是python. 如果不是,则可能是可执行文件的完整路径。


Isa*_*lla 8

VS Code 的终端使用与您选择的不同的 Python 解释器

默认情况下,它不知道您的解释器,并将使用.bashrc操作系统中的默认值或等效值进行初始化。

从 2021 年 2 月的一个问题中找到了两个相关设置。勾选第二个选项Python > Terminal: Activate Environment可以自动激活虚拟环境:

在 VS Code 中激活 Python 环境

其中settings.json称为"python.terminal.activateEnvironment": true.

更新(2021 年 8 月 8 日):今天,当我在 Windows 上从 VS Code 打开终端时,它自动插入一行代码& C:/Users/[UserName]/[venv]/Scripts/Activate.ps1来激活与所选 python 解释器关联的适当环境!看来上述设置现在是默认行为。虽然2021 年 7 月的发行说明(版本 1.59)中对终端行为进行了更改,但我没有看到明确提及虚拟环境激活。

新行为记录在此处的“环境和终端窗口”中。


"python.pythonPath" 已被弃用

因此,以前的大多数答案都已过时。改用"python.defaultInterpreterPath"

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

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

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

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

如果您希望为所有工作区设置默认的 python 解释器,请使用 打开设置Ctrl+Shift+PPreferences: Open User Settings然后搜索 Python: Default Interpreter Path. 否则,如果您只想将其设置为当前工作区,请Preferences: Open Workspace Settings改用。

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

就您而言,您希望将其设置为${workspaceFolder}/env/bin/python3.6。如果您settings.json直接编辑而不使用 GUI:

{  
  "python.defaultInterpreterPath": "${workspaceFolder}/env/bin/python3.6"
}  
Run Code Online (Sandbox Code Playgroud)

详细说明可以在文档“手动指定解释器”中找到,包括使用环境变量作为解释器的路径。


The*_*ata 7

在VSCode中,python有两个路径:

  1. 使用右上角的绿色播放按钮编写 python 代码时使用的路径。可以在 CTRL+SHIFT+P Python 下设置此路径:选择解释器。

执行Python代码

  1. 在终端中键入“python”时使用的路径,位于 Windows 10 中的“环境变量”中(Linux 和 Mac 下的位置类似)。在 Windows 10 中,您可以选择多个 Python 版本,通常位于 C:\Users\YourName\AppData\Local\Programs\Python\Python## 下。只需确保相应地更改环境变量 C:\Users\YourName\AppData\Local\Programs\Python\Python## 和 C:\Users\YourName\AppData\Local\Programs\Python\Python##\Scripts 即可。这也会影响您使用的 pip,即属于 Python 3.8 的 pip 或属于 Python 3.9 的 pip。我认为 VSCode 中的终端通常属于您的默认终端。因此,在 Windows 10 中,当您在 CMD 行中输入“python”时,它应该与 VSCode 终端版本相同。

出于理智的目的,您应该确保“Python:选择解释器”和系统环境变量都指向相同版本的 Python。

Windows 10 中的额外好东西。如果您没有设置环境变量,并且在 VSCode 终端中键入“python”,它将指向 C:\Users\YourName\AppData\Local\Microsoft\WindowsApps\python.exe,这只是在 Windows AppStore 中打开 python 链接。


Muh*_*nad 5

This solution is for Mac and Linux:

To change your Python version from 2.7 to 3 do this:

  1. In Vscode click on file > preferences > settings.

  2. On the right side click on the ... (the three dots) and select (open settings.json)

  3. In the search bar type code-runner.executorMap.

  4. You can only change the settings on the right side.

  5. After the last setting type a comma then "code-runner.executorMap" and hit enter, this will copy all the settings from the default file.

  6. 查找“ python”,并将其旁边的命令更改为“ python3”。

  7. 保存更改,您应该一切顺利。


Rus*_* A. 5

查看您的屏幕截图,我发现您正在使用Code Runner扩展程序。我想这就是您启动程序的方式。我不知道它在内部如何工作,但是将此行添加到Code-Runner扩展setting.json文件中可以解决此问题:

"code-runner.executorMap.python": "python3 -u" 
Run Code Online (Sandbox Code Playgroud)

在Code-Runner github存储库中找到它:https : //github.com/formulahendry/vscode-code-runner/issues/366

如果键入“ python --help”,您将看到“ -u”标志代表“无缓冲二进制stdout和stderr ...”-不知道为什么这很重要。