如何使用powershell/Windows终端在vscode中调试(Python)程序?

2024-09-29 00:12:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我做了一些努力来设置powershell和windows终端的主题。目前已在vscode集成终端生效

由于vscode使用powershell作为其integratedTerminal调试选项,因此我认为一定有一种方法可以使用powershell作为externalTerminal选项(defualt是cmd)。但是,当我尝试更改vscode的settings.json中的默认外部终端选项时,如下所示:

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

在尝试启动调试会话时,powershell立即弹出并消失。然后出现一条警告消息,上面写着“等待luancher连接超时”

当我尝试切换到wt.exe(Windows终端)时,vscode似乎无法识别自动生成的“\c”命令。警告来了: The following argument was not expected: \c

powershell和windows终端可执行文件都添加到路径中。 下面是我的python程序的launch.json:

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

        }
    ]
}

我想当我更改这些设置时没有出现任何问题,使用cmd.exe也没有出现任何问题。 那么,如何使用外部powershell或windows终端调试python程序呢


Tags: 程序cmdjson终端警告主题windows选项
2条回答

对于任何后来看到问题的人,如果你想做link mentioned above中提到的事情,我将其总结如下:

第一。来自VisualStudio代码

打开Settings

在搜索框中键入Terminal>external,然后在下面找到设置Terminal › External: Windows Exec

将下面的cmd路径更改为wt -p cmd cmd

第二。从Windows终端

单击窗口左上角的向下箭头,然后单击settings

一个json文件将在vscode中打开。添加下面的配置以更改配置文件。 (看看这些注释。更改注释内容为Make changes here to the ... profile.的地方。)

"startingDirectory": "%__CD__%",

这将确保您的cmd/powershell位于正确的工作目录中。 (如果您的程序包含与路径相关的工作,这一点至关重要。)

这些步骤应该在安装Windows终端、将其添加到路径(可能是自动的)并安装Visual Studio代码之后执行。 (我目前的操作系统是Windows 11,因此理论上对Windows 10用户是可行的。)

我无法让它与PowerShell完全一起工作(它会打开,但我的文件输出不会在其中,而是出现了一些关于后台作业的内容…),但使用this guide,我能够让它与Windows终端一起工作。指南指定需要将设置设置为wt -p cmd cmd以避免出现“'\c'命令无法识别”错误:

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

因此,将terminal.external.windowsExec设置为wt -p cmd cmd应该允许您使用Windows终端

相关问题 更多 >