How to debug (Python) programs in vscode with powershell/Windows Terminal?

Question:

I made some effort to set up the theme of powershell and windows terminal. Now it takes effect in integrated terminal of vscode.

Since vscode uses powershell as its integratedTerminal debugging option, I think there must be a way using powershell as the externalTerminal option(and defualt is cmd). However, when I tried to change the default external terminal option in vscode’s settings.json like this:

"terminal.external.windowsExec": "powershell.exe",

and tried to launch a debug session, the powershell just popped up and disappeared at once. Then it comes to a warning message reading "Timed out waiting for luancher to connect.".

When I tried to switch to wt.exe(Windows Terminal), it seemed that the auto-gened ‘c’ command cannot be recognized by vscode. Here comes the warning:
The following argument was not expected: c

Both powershell and windows terminal executable are added to PATH.
And here is my python program’s launch.json:

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

        }
    ]
}

I suppose nothing went wrong when I changed these settings, and nothing went wrong using cmd.exe.
So how can I debug python programs using external powershell or windows terminal?

Asked By: eicc

||

Answers:

I wasn’t able to get it to work with PowerShell fully (it would open, but my files output wouldn’t be in it, instead something about background jobs was present…), but using this guide, I was able to get it to work with Windows Terminal. The guide specified that you need to set the settings to wt -p cmd cmd to avoid the "’c’ command cannot be recognized" error:

This is because VScode assumes you are running cmd and has (afaik) no config for the command line arguments, and when they get passed to wt it has no clue what they mean, adding cmd (the last one) then makes wt run cmd and the argument get passed to it correctly.

If you want to use your profile for cmd you have to add -p cmd, so the full command becomes wt -p cmd cmd

Thus, setting terminal.external.windowsExec to wt -p cmd cmd should allow you to use Windows Terminal.

Answered By: Timothy G.

And to anyone who sees the problem later, if you want to do things mentioned in the link mentioned above, I got it summarized as below:

1st. From Visual Studio Code

Open Settings.

Type Terminal>external in the search box and find the setting Terminal › External: Windows Exec below.

Change the cmd path below to wt -p cmd cmd.

2nd. From Windows Terminal

Click the downward arrow on top left of the window, then click settings.

A json file would be open in vscode. Add the config below to change the profile.
(Look at those comments. Change things where the comment reads Make changes here to the ... profile..)

"startingDirectory": "%__CD__%",

This ensures your cmd/powershell in right working directory.
(If your program contains path-related work, it is crucial.)

These steps are supposed to be taken after Windows Terminal is intalled, added to PATH(maybe automatically), and Visual Studio Code is installed.
(My current OS is Windows 11, so is theoretically feasible for Windows 10 users.)

Answered By: eicc

You can change your default terminal temporarily by clicking F1 button on your keyboard, then choosing Terminal: Select Default Profile. Choose Powershell as your default profile then re-run the debugger. It will run automatically on Powershell.

These are my defualt launch.json settings (I left them untouched):

{
  "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true
        }
    ]
}
Answered By: ivey221