且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 VSCode 在 Python 中调试期间读取输入

更新时间:2022-02-23 09:17:48

技巧这个工作在扩展的(Don Jayamanne的Python) wiki上页.您必须在 launch.json 文件的 "name": "Python" 部分中包含 "externalConsole": true 设置.

The trick to getting this to work is on the extension's(Don Jayamanne's Python) wiki page. You have to include "externalConsole": true setting in your launch.json file's "name": "Python" section.

扩展的维基确认这在默认情况下不起作用:

The extension's wiki confirms that this does not work by default:

这允许从控制台/终端窗口捕获输入应用程序,这在标准 VSCode 调试器中是不可能的.

This allows for capturing of input from the console/terminal window applications, which isn't possible in the standard VSCode debugger.

以下是使其工作的步骤:

Here are the steps to getting this to work:

  1. 在调试窗口 (Ctrl+Shift+D) 中,按小齿轮图标打开(或生成)launch.json 文件.它被放置到 .vscode 目录中,您在 VS Code 中选择作为打开文件夹"的任何文件夹.
  2. 您必须将 pythonPath 参数添加到第一个配置块.这是让调试器工作所必需的.
  3. 您还必须将 externalConsole 参数添加到同一块中.这是让调试器接受输入所需要的.调试时,会在 VS Code 之外打开一个单独的窗口,但在其他情况下运行良好.
  4. 添加两个设置后,块应该看起来像这样.我不必更改 launch.json 文件的其余部分中的任何其他内容.

  1. From the Debug window (Ctrl+Shift+D), press the little gear icon to open (or to generate) a launch.json file. It gets placed into a .vscode directory in what ever folder you have selected as your "Open Folder" in VS Code.
  2. You have to add pythonPath parameter to the first configuration block. This is needed to have the debugger work at all.
  3. You also have to add and externalConsole parameter to the same block. This is what is needed to have the debugger accept input. When you debug, a separate window will open outside of VS Code but works well otherwise.
  4. After you add both settings, the block should look something like this. I did not have to change anything else in the rest of the launch.json file.

{
    "name": "Python",
    "type": "python",
    "request": "launch",
    "stopOnEntry": true,
    "program": "${file}",
    "pythonPath": "C:/Users/igor/Documents/Tools/WinPython-32bit-3.4.3.7Slim/python-3.4.3/python.exe",
    "externalConsole": true,
    "debugOptions": [
        "WaitOnAbnormalExit",
        "WaitOnNormalExit",
        "RedirectOutput"
    ]
},