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.
Recent versions of VS Code (~v1.60 and later) have reworked some of the core testing infrastructure, and this might not be as big of a problem. However, I'm still running into it occasionally.
One thing that normally works, even without the below, seems to be to avoid the "restart debugging" button. If you just stop the debugging session and then restart however you started it in the first place, I rarely get the multiple session windows.
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:
launch.json
file in an editor window.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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With