debugging Visual Studio代码- Python调试-在执行时单步执行外部函数的代码

epggiuax  于 2022-11-14  发布在  Python
关注(0)|答案(5)|浏览(152)

在Python项目中,如何让内置的VSCode调试器在执行时单步执行来自其他库的函数代码?
我知道在标准库中实现的函数可以通过添加

"debugOptions": ["DebugStdLib"]

到您在 launch.json 中的配置(如指定的here),但是似乎不可能强制调试器单步执行非标准模块的代码,例如您自己编写并导入到当前文件中的代码。

falq053o

falq053o1#

为了改进John Smith的公认答案,值得一提的是,现在该选项又重新命名了,新选项为

"justMyCode": false

根据文件
如果省略或设置为True(默认值),则仅对用户编写的代码进行调试。如果设置为False,则还启用标准库函数的调试。

pkbketx9

pkbketx92#

这是通过自定义调试器来完成的。
如果您还没有,您需要初始化调试器定制。您可以通过打开边栏中的调试器部分并选择create a launch.json file来完成此操作。
完成此操作后,将在工作区的.vscode文件夹中创建一个launch.json文件。
编辑此文件。它将类似于:

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

"justMyCode": false添加到"Python: Current File"配置中,如下所示:

{
    ...,
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": false
        }
    ]
}

自Visual Studio代码版本1.59.0起为True。
参考:https://code.visualstudio.com/docs/python/debugging

avwztpqn

avwztpqn3#

调试器配置

"debugOptions": ["DebugStdLib"]

实际上,将步入用户定义和pip安装的模块,这与主要问题中所写的相反。

llycmphe

llycmphe4#

大多数时候,我调试单元测试,而不是运行的应用程序。
如果您这边也是这种情况,并且您用途:

然后使用以下launch.json,如插件页面中所述:

{
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Debug test",
                "type": "python",
                "request": "attach",
                "console": "externalTerminal",
                "justMyCode": false,
                "stopOnEntry": true,
                "envFile": "${workspaceFolder}/.env.test",
                "purpose": ["debug-test"]
            }
        ]
    }
6rvt4ljy

6rvt4ljy5#

对于那些使用标准VSCode调试器的用户,可能需要对如何在VS代码中进行配置进行更多的说明

按如下所示创建调试器配置

打开您的.vscode/launch.json
将配置{}添加到配置列表:

"configurations": []

内建的VSCode调试工具会辨识这个程式码:

{
  "name": "Python: Debug Tests",
  "type": "python",
  "request": "launch",
  "program": "${file}",
  "purpose": ["debug-test"],
  "console": "integratedTerminal",
  "justMyCode": false
}

要点:

  1. purpose应设置为[“调试-测试”]
  2. "justMyCode":应设置为false。
    参考:官方VSCode文档

相关问题