简体   繁体   English

如何让 VS Code 将 Debug stdout 写入调试控制台?

[英]How do you get VS Code to write Debug stdout to the Debug Console?

I am trying to debug my Python Pytest tests in VS Code, using the Testing Activity on the left bar.我正在尝试使用左侧栏中的测试活动在 VS Code 中调试我的 Python Pytest 测试。 I am able to run my tests as expected, with some passing and some failing.我能够按预期运行我的测试,有些通过,有些失败。 I would like to debug the failing tests to more accurately determine what is causing the failures.我想调试失败的测试以更准确地确定导致失败的原因。

When I run an individual test in debug mode VS Code is properly hitting a breakpoint and stopping, and the Run and Debug pane shows the local variables.当我在调试模式下运行单个测试时,VS Code 正确地到达断点并停止,并且“运行和调试”窗格显示局部变量。 I can observe the status of local variables either in the Variables > Local pane or through the REPL, by typing the name of the variable.我可以在“变量”>“局部”窗格中或通过 REPL,通过键入变量的名称来观察局部变量的状态。

When I try to print out any statement, such as using > print("here") I do not get any output to the Debug Console.当我尝试打印出任何语句时,例如使用> print("here")我没有得到任何输出到调试控制台。 When I reference a variable, or put the string directly using > "here" I do see the output to the Debug Console.当我引用一个变量,或者直接使用> "here"放置字符串时,我确实看到了调试控制台的输出。

It seems to me that the stdout of my REPL is not displaying to the Debug Console.在我看来,我的 REPL 的标准输出没有显示到调试控制台。 A number of answers online have been suggesting to add options like "redirectOutput": true or "console": "integratedTerminal" , but neither of those seem to have worked.网上的许多答案都建议添加诸如"redirectOutput": true"console": "integratedTerminal"类的选项,但这些选项似乎都没有奏效。 My full launch.json is below:我的完整launch.json如下:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "debugOptions": [
                "WaitOnAbnormalExit",
                "WaitOnNormalExit"
            ],
            "console": "integratedTerminal",
            "stopOnEntry": false,
            "redirectOutput": true,
            "outputCapture": "std"
        }
    ]
}

Is there another setting I'm missing to enable this output?我是否缺少另一个设置来启用此输出? Have I got the wrong console type?我有错误的控制台类型吗?

So After a lot of frustrating "debugging" I found a solution that worked for me (if you are using pytest as me):所以在经历了很多令人沮丧的“调试”之后,我找到了一个对我有用的解决方案(如果你像我一样使用pytest ):

tldr tldr

Two solutions:两种解决方案:

  1. downgrade your vscode python extension to v2022.2.1924087327 that will do the trick (or any version that had the debugpy<=1.5.1 ).将您的 vscode python 扩展降级到v2022.2.1924087327可以解决问题(或任何具有debugpy<=1.5.1的版本)。
    在此处输入图像描述

  2. Or, Launch the debbuger from the debug tab not the testing tab .或者,从debug tab而不是testing tab启动调试器。 And use a configuration like the following one并使用如下配置

    { "name": "Python: Current File (Integrated Terminal)", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal", "purpose": ["debug-test"], "redirectOutput": true, "env": {"PYTHONPATH": "${workspaceRoot}"} }
  3. Bonus .奖金 If you are using pytest you can temporarily disable the capture of the stdout of pytest so your print statements, and the print function, *if you breakpoint inside the contextmanager , will work too.如果您使用的是pytest ,您可以暂时禁用对 pytest 的标准输出的捕获,以便您的打印语句和print功能,*如果您在contextmanager中设置断点,也可以工作。 This is very cumbersome but points out the original problem of why the prints are not longer working.这非常麻烦,但指出了为什么打印不再工作的原始问题

     def test_disabling_capturing(capsys): print('this output is captured') with capsys.disabled(): print('output not captured, going directly to sys.stdout') print('this output is also captured')

the long explanation长的解释

so the problem apparently is that the debugpy (which is the library used by vscode python debugger) in is last version v1.6.0 fixed this "bug (827)" .所以问题显然是debugpy (这是vscode python调试器使用的库)是最新版本v1.6.0修复了这个“bug(827)” In a nutshell, this "bug" was that vscode "duplicated" all the stdout when debugging because it captures the pytest stdout and replicate it in the debugger console.简而言之,这个“错误”是 vscode 在调试时“复制”了所有标准输出,因为它捕获了pytest stdout并将其复制到调试器控制台中。 This is because, by default, pytest captures all the stdout and store it (so when running all test in parallel it doesn't create a mess).这是因为,默认情况下, pytest捕获所有标准输出并将其存储(因此在并行运行所有测试时不会造成混乱)。

After "fixing" this issue, now, when you launch the test via the testing tab , by default, pytest is capturing all the stdout and the "new" ( >=v1.6.1 ) debugpy ignores it.在“修复”这个问题之后,现在,当您通过testing tab启动测试时,默认情况下, pytest会捕获所有stdout ,而“新”( >=v1.6.1 debugpy忽略它。 Therefore, all the print statements are not shown anymore on the debug console, even when you call print() in a breakpoint, because are captured by pytest (IDK where the pytest captured stdout is showing/stored if it is anywhere).因此,调试控制台上不再显示所有打印语句,即使您在断点中调用print()也是如此,因为它们被 pytest 捕获(如果它在任何地方,则显示/存储pytest捕获的标准输出的 IDK)。 which, in my case is a PITA.就我而言,这是一个 PITA。

You can disable the pytest capture option using the flag -s or --capture=no when launching pytest in a console or even from the debug tab as a custom configuration.您可以使用标志-s--capture=no在控制台中启动 pytest 或什至从调试选项卡作为自定义配置来禁用pytest捕获选项。 but the problem is that there is no way (apparently) to add these parameters in vscode for the testing tab so pytest is executed using that option.但问题是(显然)无法在 vscode 中为testing tab添加这些参数,因此使用该选项执行 pytest。

Therefore the solution that I found was to downgrade the python extension to a version that uses an older version of debugpy v1.5.1 , you can see in the python extension changelog that from the version 2022.4.0 they update the debugpy version, so going before that did the trick for me, you will have the double stdout "bug" in the console, but the print statement will work.因此,我找到的解决方案是将 python 扩展降级到使用旧版本debugpy v1.5.1的版本,您可以在python 扩展更改日志中看到,他们从2022.4.0版本更新了debugpy版本,所以之前这对我有用,您将在控制台中有双重stdout “错误”,但print语句将起作用。

ref: The issue that lead me to the solution参考:导致我解决问题的问题


You may make your voice heard here in the debugpy issues to encourage fixing it perhaps: https://github.com/microsoft/debugpy/issues/947您可以在 debugpy 问题中听到您的声音,以鼓励修复它: https ://github.com/microsoft/debugpy/issues/947

One possible workaround I saw online is to print the DF in Spark to a string, then paste it into Notepad++ and convert the \n values into newlines.我在网上看到的一种可能的解决方法是将 Spark 中的 DF 打印为字符串,然后将其粘贴到 Notepad++ 中并将 \n 值转换为换行符。

I was able to print the DF to a string using some code I found on another Stack Overflow answer:我能够使用在另一个 Stack Overflow 答案中找到的一些代码将 DF 打印到字符串中:

def getShowString(df, n=20, truncate=True, vertical=False):
    if isinstance(truncate, bool) and truncate:
        return(df._jdf.showString(n, 20, vertical))
    else:
        return(df._jdf.showString(n, int(truncate), vertical))

This can then be called using getShowString(df)然后可以使用getShowString(df)调用它

This does not solve the actual issue of getting terminal output directed to the debug window, but can help in this particular case.这并不能解决将终端输出定向到调试窗口的实际问题,但在这种特殊情况下会有所帮助。

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

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