简体   繁体   English

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

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

I made some effort to set up the theme of powershell and windows terminal.我做了一些努力来设置 powershell 和 windows 终端的主题。 Now it takes effect in integrated terminal of vscode.现在在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).由于 vscode 使用 powershell 作为其integratedTerminal终端调试选项,我认为必须有一种方法使用 powershell 作为externalTerminal终端选项(默认为 cmd)。 However, when I tried to change the default external terminal option in vscode's settings.json like this:但是,当我尝试更改 vscode 设置中的默认外部终端选项时。json 如下所示:

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

and tried to launch a debug session, the powershell just popped up and disappeared at once.并尝试启动调试 session,powershell 刚刚弹出并立即消失。 Then it comes to a warning message reading "Timed out waiting for luancher to connect.".然后它会出现一条警告消息,上面写着“等待 luancher 连接超时。”。

When I tried to switch to wt.exe(Windows Terminal), it seemed that the auto-gened '\c' command cannot be recognized by vscode.当我尝试切换到 wt.exe(Windows 终端)时,vscode 似乎无法识别自动生成的 '\c' 命令。 Here comes the warning: The following argument was not expected: \c出现警告: The following argument was not expected: \c

Both powershell and windows terminal executable are added to PATH. powershell 和 windows 终端可执行文件都添加到 PATH。 And here is my python program's launch.json:这是我的 python 程序的启动。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.我想当我更改这些设置时没有出现任何问题,使用 cmd.exe 也没有出现任何问题。 So how can I debug python programs using external powershell or windows terminal?那么如何使用外部 powershell 或 windows 终端调试 python 程序呢?

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.我无法让它完全与 PowerShell 一起使用(它会打开,但我的文件输出不在其中,而是存在一些有关后台作业的信息......),但是使用本指南,我能够让它与 Windows 终端一起工作。 The guide specified that you need to set the settings to wt -p cmd cmd to avoid the "'\\c' command cannot be recognized" error:该指南指定您需要将设置设置为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.这是因为 VScode 假设您正在运行cmd并且(afaik)没有命令行参数的配置,并且当它们传递给wt它不知道它们的含义,添加cmd (最后一个)然后使wt run cmd和参数正确传递给它。

If you want to use your profile for cmd you have to add -p cmd , so the full command becomes wt -p cmd cmd如果你想使用你的cmd配置文件,你必须添加-p cmd ,所以完整的命令变成了wt -p cmd cmd

Thus, setting terminal.external.windowsExec to wt -p cmd cmd should allow you to use Windows Terminal.因此,将terminal.external.windowsExec设置为wt -p cmd cmd应该允许您使用 Windows 终端。

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来自 Visual Studio 代码

Open Settings .打开Settings

Type Terminal>external in the search box and find the setting Terminal › External: Windows Exec below.在搜索框中键入Terminal>external ,然后在下面找到设置Terminal › External: Windows Exec

Change the cmd path below to wt -p cmd cmd .将下面的 cmd 路径更改为wt -p cmd cmd

2nd.第二。 From Windows Terminal从 Windows 终端

Click the downward arrow on top left of the window, then click settings .单击窗口左上角的向下箭头,然后单击settings

A json file would be open in vscode.一个json文件将在 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. .) (看看那些评论。更改评论中的内容Make changes here to the ... profile. 。.)

"startingDirectory": "%__CD__%",

This ensures your cmd/powershell in right working directory.这可确保您的 cmd/powershell 位于正确的工作目录中。 (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.这些步骤应该在安装 Windows 终端、添加到 PATH(可能会自动)并安装 Visual Studio Code 之后执行。 (My current OS is Windows 11, so is theoretically feasible for Windows 10 users.) (我当前的操作系统是 Windows 11,所以理论上对于 Windows 10 用户是可行的。)

You can change your default terminal temporarily by clicking F1 button on your keyboard, then choosing Terminal: Select Default Profile .您可以临时更改默认终端,方法是单击键盘上的F1按钮,然后选择Terminal: Select Default Profile Choose Powershell as your default profile then re-run the debugger.选择 Powershell 作为默认配置文件,然后重新运行调试器。 It will run automatically on Powershell.它将在 Powershell 上自动运行。

These are my defualt launch.json settings (I left them untouched):这些是我的默认 launch.json 设置(我没有动过它们):

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

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

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