Reading input during debugging in Python with VSCode

后端 未结 7 2073
执笔经年
执笔经年 2020-12-05 07:53

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 no

相关标签:
7条回答
  • 2020-12-05 08:14

    In Visual Studio Code click the pick list to the right of the green arrow. Then select Python: Terminal (external). When you launch your script it will run in an external window and allow you to key in input.

    0 讨论(0)
  • 2020-12-05 08:15

    Change the launch.json and put this into your java code

    {
        "type": "java",
        "name": "Debug (Launch)",
        "request": "launch",
        "cwd": "${workspaceFolder}",
        "console": "externalTerminal",
        "stopOnEntry": false,
        "mainClass": "",
        "args": ""
    }
    
    0 讨论(0)
  • 2020-12-05 08:17

    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:

    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. 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"
          ]
      },
      
    0 讨论(0)
  • 2020-12-05 08:18

    Updated Information on Console Options

    Most of the higher-rated, original answers are no longer valid, or unclear exactly what to set and how. See below for details how to set the "Console" option in launch.json, and what all the options are.

    You can choose either an internal or external terminal setting and have keyboard input work during debug. One option for output, the Debug Console, does not currently (Fall 2019) allow keyboard input to your program, although you can always use the debug console to enter live debug and code commands.

    The steps to set the available options are below.


    Opening the Debug Configuration launch.json file

    1. Click the debug icon (update early 2020 - now the "Run" icon): to open the debug sidebar (again, now called the "Run" sidebar, and the command menu name is also changed from debug to run).
    2. At the top of the screen, ensure "Python: Current File" is selected. You may need to select it or create it (might need to create your first debug/run configuration):
    3. Click the gear icon to the right of the configuration dropdown selected in prior step. This will bring up the launch.json for that configuration in the editor.
    4. Update the "console": option to one of the settings described below

    Valid "console" settings in launch.json

    • "console": "internalConsole"

      • this is the default setting
      • uses the internal debug console
      • as of 10/2019 does not allow keyboard input.
    • "console": "integratedTerminal"

      • this spawns a new Python Debug Console terminal window every time you debug (I wish it would reuse any existing one, but it doesn't - use the trash can symbol on the upper right of the terminal window to remove old, unused terminals)
      • The type of terminal created is based on the default terminal type you setup (i.e. command window, bash shell, etc.).
      • All standard output will be in this terminal and you can enter keyboard input if the program is waiting for it.
      • You can switch to the DEBUG CONSOLE tab if you want to run command during debugging.
    • "console": "externalTerminal"

      • this spawns a separate terminal outside of the VS Code process as the terminal for your code to run in on run or debug.
      • the external terminal will be the default type for your OS (command window for Windows 10).
      • this terminal is separate from VS Code and will normally add a Press any key to continue... prompt after your program terminates so that you can view/copy any output before it disappears.
      • All standard output will go to that terminal and keyboard input can be entered in it.
      • You can switch to the DEBUG CONSOLE in VS Code when the code is paused to enter debug commands.
    0 讨论(0)
  • 2020-12-05 08:24

    The console option could have any of these values: internalConsole, integratedTerminal, externalTerminal.

    Normally if you start the debugger and the program stops, it leaves the external terminal displaying a prompt Press Enter to continue . . . to have access to any output of the program. If you accidentally have a syntax error the external terminal just closes not leaving any message.

    When using the integratedTerminal option, the terminal stays there and displays the error message.

    While I do not know if this externalTerminal thing is a bug or not, the integratedTerminal option seems to work much better in this case.

    0 讨论(0)
  • 2020-12-05 08:31

    The externalconsole directive is deprecated. Use console instead and indicate your preference for external this way:

    "console": "externalTerminal"
    

    The application output (and input) will go to a separate window so the VS Code debug console remains a pure python prompt where you can evaluate stuff during breakpoints.

    0 讨论(0)
提交回复
热议问题