如何在 Visual Studio Code 中调试 Python3 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36859564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:26:59  来源:igfitidea点击:

How can I debug Python3 code in Visual Studio Code

pythonpython-3.xdebuggingvisual-studio-code

提问by elzapp

I want to debug a project written in python3 in Visual Studio Code, but I can't seem to find any way of specifying interpreter or python version in the launch.json file.

我想在 Visual Studio Code 中调试一个用 python3 编写的项目,但我似乎无法在 launch.json 文件中找到任何指定解释器或 python 版本的方法。

It works great for Python 2, so the question is, how do I make it work for Python 3?

它适用于 Python 2,所以问题是,我如何使它适用于 Python 3?

回答by Daniel F.

Since I cannot comment to the accepted answer I'll copy some of my answerto a similar question.

由于我无法对已接受的答案发表评论,因此我会将我的一些答案复制到类似问题中。

As of September 2016 (according to the Github repo documentation of the extension) you can just execute a command from within vscode that will let you select the interpreter from an automatically generated list of known interpreters (including the one in your project's virtual enviroment).

截至 2016 年 9 月(根据扩展的 Github 存储库文档),您只需从 vscode 中执行一个命令,即可让您从自动生成的已知解释器列表(包括项目虚拟环境中的解释器)中选择解释器。

Execute:

执行:

Python: Select Workspace Interpreter

UpdateThis command has been updated to just:

更新此命令已更新为:

Python: Select Interpreter

in the command pallet (F1 for Windows, Ctrl+Shift+P for MacOS).

在命令托盘中(Windows 为 F1,MacOS 为 Ctrl+Shift+P)。

Then select one of the python interpreters shown in a drop down list. And that's it. Your settings.json will be edited automatically to point to the interpreter you selected.

然后选择下拉列表中显示的 Python 解释器之一。就是这样。您的 settings.json 将被自动编辑以指向您选择的解释器。

Source: Don Jayamanne's extension's documentation at Github

来源:Github 上 Don Jayamanne 的扩展文档

回答by Igor

Python3 debugging works well also. It is a little confusing as there are two different places to specify the path: settings.jsonand launch.json. I recommend using Don Jayamanne's Python Extension. After installing it, you have to configure the pathto the interpreter you want to use it with.

Python3 调试也很有效。这有点令人困惑,因为有两个不同的地方可以指定路径:settings.jsonlaunch.json。我建议使用Don Jayamanne 的 Python 扩展。安装后,您必须配置要使用它的解释器的路径

Python Version used for Intellisense, Autocomplete, Linting, Formatting, etc

The same python interpreter is used for intellisense, autocomplete, linting, formatting, etc. (everything other than debugging). The standard interpreter used is the first "python" interpreter encountered in the current path. If a different version is to be used, this can be configured in one of two ways:

Configure the path to the python interpreter in the User Settings file (settings.json) as follows. Ensure to specify the fully qualified name of the python executable. "python.pythonPath": "c:/python27/python.exe"

Configure the path to the python interpreter in the Workspace Settings file (settings.json) as follows. Ensure to specify the fully qualified name of the python executable. "python.pythonPath": "c:/python27/python.exe" Python Version used for debugging

Details on configuration settings for debugging can be found here Debugging. Simply provide the fully qualified path to the python executable in the "python" setting within the configuration settings in the launch.json file as follows:

用于 Intellisense、自动完成、Linting、格式化等的 Python 版本

相同的 Python 解释器用于智能感知、自动完成、linting、格式化等(除调试之外的所有内容)。使用的标准解释器是当前路径中遇到的第一个“python”解释器。如果要使用不同的版本,可以通过以下两种方式之一进行配置:

在用户设置文件(settings.json)中配置python解释器的路径如下。确保指定 python 可执行文件的完全限定名称。"python.pythonPath": "c:/python27/python.exe"

在工作区设置文件 (settings.json) 中配置 python 解释器的路径,如下所示。确保指定 python 可执行文件的完全限定名称。"python.pythonPath": "c:/python27/python.exe" 用于调试的 Python 版本

有关调试的配置设置的详细信息可以在此处找到调试。只需在 launch.json 文件的配置设置中的“python”设置中提供 python 可执行文件的完全限定路径,如下所示:

{
    "name": "Python",
    "type": "python",
    "request": "launch",
    "stopOnEntry": true,
    "program": "${file}",
    "pythonPath": "c:/python27/python.exe",
    "debugOptions": [
        "WaitOnAbnormalExit",
        "WaitOnNormalExit",
        "RedirectOutput"
    ] }

回答by Aleaxandors Cen

There is a setting trigger within the setting file:

设置文件中有一个设置触发器:

for python default(which is 2.7 for now)

对于 python 默认(现在是 2.7)

"python.pythonPath": "python",

for python3:

对于python3:

"python.pythonPath": "python3",

use the follow command to check the python version:

使用以下命令检查python版本:

import sys
  print(sys.version)

回答by Revol89

We can configure the debug in python3 in settings.json:

我们可以在settings.json 中配置 python3 中的调试:

File > Preferences > Settings (~/.config/Code/User/settings.json) [User Settings]

文件 > 首选项 > 设置 (~/.config/Code/User/settings.json) [用户设置]

{
  ...
  "python.pythonPath": "python3",
}

Also, verify that the launch.jsonfile already has the following configuration:

此外,请验证launch.json文件是否已具有以下配置:

"configurations": [
  {
    "name": "Python: Current File (Integrated Terminal)",
    "type": "python",
    "request": "launch",
    "program": "${file}",
    "console": "integratedTerminal"
  },
  ...
]

回答by John T

An extra note for those using the anaconda python distribution by continuum analytics; you may find my experience useful.

对于通过连续分析使用 anaconda python 发行版的人的额外说明;你可能会发现我的经验很有用。

I'm using Don Jayamanne's Python Extension and run the "Select Workspace Interpreter" command, but still found I was getting linting advice for the wrong version of python.

我正在使用 Don Jayamanne 的 Python 扩展并运行“选择工作区解释器”命令,但仍然发现我收到了错误版本的 Python 的 linting 建议。

The fix that worked for me was installing the pylint package for anaconda.

对我有用的修复程序是为 anaconda 安装 pylint 包。

conda install -c anaconda pylint