在 VSCode 中,Python 调试器每次调试时都会启动一个新的终端 [英] In VSCode, Python debugger launches a new Terminal every time I debug

查看:57
本文介绍了在 VSCode 中,Python 调试器每次调试时都会启动一个新的终端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Python 中调试时,在 VS Code 中,每次调试时都会创建一个新终端.代码只是每次都在终端窗口的下拉列表中添加一个终端.我必须手动删除每个终端,或者一段时间后删除一堆 - 否则代码最终会挂起.

是否有选项设置可以阻止这种情况?这是预期行为还是缺陷?


更新:这是每次创建新调试终端时发生的情况的屏幕截图.这是终端窗口右侧的下拉菜单,您可以使用 ctrl-`(坟墓键、非移位波浪号或 ~ 键)打开或转到该下拉菜单.它显示了普通的 bash 终端,一个每次运行脚本时都会重复使用的 Python 终端,但显示了 3 个 Python 调试控制台 窗口.每次调试时都会创建一个新的 Python 调试控制台 (F5).所以我每次调试时都需要进去手动删除一个Python调试控制台(点击右边的垃圾桶图标).在我意识到它发生之前,它打开了多达 20 多个终端窗口.

解决方案

一个真正的解决方案:之后让终端退出!

终于看到了一个真正的解决方案(好吧,有点笨拙),

  1. args: ["&", "exit"] 添加如下所示的行,并确保在该行的末尾添加逗号!!

launch.json:

{//使用 IntelliSense 了解可能的属性.//悬停以查看现有属性的描述.//更多信息,请访问:https://go.microsoft.com/fwlink/?linkid=830387版本":0.2.0",配置":[{名称":Python:当前文件",类型":蟒蛇",请求":启动",程序":${file}",控制台":集成终端",参数":[&&",退出"]}]}

注意 1: 该答案留下的评论表明您可能想要尝试 "args": ["\n", "exit", "0"] 如果这不起作用.这可能适用于不同的终端类型(Windows Cmd Prompt、PowerShell、不同的 Linux shell 等).

注意 2: 如果您需要添加其他参数,您可以将它们作为字符串添加到&&"之前.列表中的参数.列表中较早位置的项目将成为您实际程序/脚本的参数.



替代(原始)解决方案:使用调试控制台进行输出

经过一番搜索后,我无法确定是否为每次调试启动一个新终端是预期行为,但有一个解决方法.

Python:当前文件设置调试配置.在调试选项卡的顶部,单击齿轮图标以打开 launch.json

注意: 下面的调试图标略有变化,此选项卡现在称为 RunDebug

launch.json 中,将 console" 设置从 integratedTerminal" 的默认设置更改为 ";internalConsole",如下图:

{ "version": "0.2.0",配置":[{名称":Python:当前文件",类型":蟒蛇",请求":启动",程序":${file}",控制台":内部控制台"}]}

这将导致任何调试会话的所有输出都只发生在调试控制台中,每个会话都会被清除和重用,而不是每个会话都生成一个新的集成终端.


缺点

我最终返回到集成终端以获取需要用户在控制台输入的脚本,因为调试控制台不允许用户输入.

在这种情况下,您只需要不断删除额外的调试会话 - 有点麻烦.

When debugging in Python, in VS Code, it is creating a new terminal every time I debug. Code just keeps adding a terminal every time to the dropdown list in the terminal window. I have to manually delete each terminal, or delete a bunch of them after a while - otherwise Code eventually hangs.

Is there an option setting to stop this? Is it expected behavior or a defect?


Update: Here's a screenshot of what is happening as a new debug terminal is created each time. This is the dropdown on the right side of the terminal window that you can open or go to with ctrl-` (the grave key, the non-shifted tilde or ~ key). It shows the normal bash terminal, a Python terminal that gets reused every time you run a script, but 3 Python Debug Console windows. A new Python Debug Console gets created every time you debug (F5). So I need to go in and manually delete a Python Debug Console (hit the garbage can icon on the right) every time I debug. This was getting up to 20+ terminal windows open before I realized it was happening.

解决方案

A Real Solution: Get the terminal to exit afterward!

Finally saw a real solution to this (well, a little hacky), in this answer - at least if you are using Git Bash as your default terminal.

If you add arguments && and exit to the debug configuration, the debug terminal will automatically exit after your program ends. Be warned though, it will immediately close the terminal, and all the text in it (you might need to add a "Press any key to end program" at the end of your script to give you time to review any text, or something like that).

Note: This is still a hack and it doesn't always work - if you hit the "restart" or "stop" buttons on the debugger toolbar, it will shortcut this method

The && basically tells Bash to stop and wait for the debug task to finish before continuing with additional commands, and then the exit will execute after your debug session ends, which closes the terminal.

You can do this by opening your run/debug configuration as follows:

  1. Go to the "Run" window in the sidebar
  2. Select the run configuration in the dropdown then press the gear, which will bring up the corresponding launch.json file in an editor window.

  1. Add the line for args: ["&&", "exit"] as shown below, and MAKE SURE YOU ADD A COMMA AT THE END OF THE LINE ABOVE THAT!!

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}",
            "console": "integratedTerminal",
            "args": ["&&", "exit"]
        }
    ]
}

NOTE 1: A comment left at that answer indicates you might want to try "args": ["\n", "exit", "0"] if that doesn't work. This is likely for a different terminal type (Windows Cmd Prompt, PowerShell, different Linux shell, etc.).

NOTE 2: If you need to add other arguments, you can add them as strings before the "&&" argument in the list. Items placed earlier in the list will become arguments to your actual program/script.



Alternative (Original) Solution: Use the Debug Console for output

After some searching, I cannot determine if it is expected behavior to launch a new terminal for each debug, but there is a workaround.

Setup your debug configuration for Python: Current File. On the debug tab, at the top, click the gear icon to open launch.json

Note: The debug icon below changed slightly and this tab is now called Run instead of Debug

In launch.json, change the "console" setting from the default of "integratedTerminal" to "internalConsole", as shown below:

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

This will cause all output of any debug session to only happen in the DEBUG CONSOLE, which gets cleared and reused each session, rather than spawning a new integrated terminal every session.


The downside

I ended up going back to the integrated terminal for scripts that expect user input at the console, because the debug console does not allow user input.

In that case, you just need to constantly delete the extra debug sessions - a bit of a pain.

这篇关于在 VSCode 中,Python 调试器每次调试时都会启动一个新的终端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆