使用 VSCode 在 Python 中调试期间读取输入 [英] Reading input during debugging in Python with VSCode

查看:54
本文介绍了使用 VSCode 在 Python 中调试期间读取输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在 vs 代码中使用的 python 扩展:python 扩展.

Here is the extension for python I used in the vs code: python extension.

当我使用扩展提供的调试功能时,如果它需要从命令行输入,它会挂在那里并且什么也不做.

When I use the debugging feature provided by the extension, it will hang in there and do nothing if it needs input from the command line.

我在哪里可以输入值来跳过 vs 代码中的输入语句?

Where can I input values to step over the input statement in the vs code?

推荐答案

技巧这个工作在扩展的(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"
    ]
},

这篇关于使用 VSCode 在 Python 中调试期间读取输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆