Reading input during debugging in Python using VSCode

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

When I use the debugging feature provided by the extension, it will freeze and do nothing if it requires command line input.

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

+12
source share
8 answers

the trick to getting this to work is on the extension ( Don Jayamanne Python ). You must include "externalConsole": true in your launch.json file "name": "Python" .

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

This allows capturing input from the console / terminal window, which is not possible in the standard VSCode debugger.

Here are the steps to help you do this:

  • In the debug window (Ctrl + Shift + D), click the small gear icon to open (or generate) the launch.json file. It is placed in the .vscode directory in the folder that you selected as the "open folder" in VS code.
  • You need to add the pythonPath parameter to the first configuration block. This is necessary for the debugger to work in general.
  • You also need to add the externalConsole parameter to the same block. This is what the debugger needs to accept input. When debugging, a separate window opens outside the VS code, but otherwise works well.
  • After adding both settings, the block should look something like this. I did not have to change anything 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" ] }, 
+12
source

The externalconsole directive has been deprecated. Use console instead and specify your preference for the external:

 "console": "externalTerminal" 

The application exit (and input) will go into a separate window so that the VS Code debugging console remains a pure python hint where you can evaluate material during breakpoints.

+10
source

VS Code has debugging capabilities using the Python console.

Just press Ctrl + Shift + D and next to the blue play icon, press the down arrow and select the Python Console App instead of just Python , for example:

enter image description here

+2
source

The console parameter can have any of these values: internalConsole, integratedTerminal, externalTerminal .

Usually, if you start the debugger and the program stops, it leaves an external terminal that displays the Press Enter to continue . . . prompt Press Enter to continue . . . Press Enter to continue . . . to access any program output. If you accidentally receive a syntax error, the external terminal simply closes without leaving any message.

When using the integratedTerminal parameter, the terminal remains there and displays an error message.

While I do not know if this external external thing is an error or not, in this case the IntegratedTerminal function works much better.

+2
source

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

0
source

Modify the launch.json file and paste it into your Java code.

 { "type": "java", "name": "Debug (Launch)", "request": "launch", "cwd": "${workspaceFolder}", "console": "externalTerminal", "stopOnEntry": false, "mainClass": "", "args": "" } 
0
source

This is tagged P2 on GitHub. You can vote for this question on Github.

Github issue

0
source

You can select the internal or external settings of the terminal and use the keyboard input during debugging. Setup instructions and available options are provided below.


Opening launch.json debug configuration file

  1. Click the debug icon enter image description here to open the debug sidebar
  2. At the top of the screen, make sure Python: Current File is selected. You may need to select it or create it (you may need to create the first debug / launch configuration): enter image description here
  3. Click the gear icon to the right of the drop-down list of the configuration selected in the previous step. This will invoke the launch.json file for this configuration in the editor.
  4. Update the "console": option "console": to one of the settings described below enter image description here

Valid "console" settings in launch.json

  • "console": "internalConsole"

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

    • this spawns a new Python Debug Console terminal window every time you debug (I would like it to reuse any existing one, but it isn’t - use the trash symbol in the upper right corner of the terminal window to remove old, unused terminals)
    • The type of terminal you are creating depends on the default terminal type you set (for example, a command window, bash shell, etc.).
    • All standard output will be in this terminal, and you can enter keyboard input if the program expects it.
    • You can switch to the DEBUG CONSOLE tab if you want to execute the command during debugging. enter image description here
  • "console": "externalTerminal"

    • this spawns a separate terminal outside the VS Code process as a terminal to run your code on startup or debugging.
    • the external terminal will be the default type for your OS (command window for Windows 10).
    • this terminal is separate from the VS code and usually adds a hint Press any key to continue... after completing your program so that you can view / copy any output before it disappears.
    • All standard output data will be transmitted to this terminal, and keyboard input can be entered into it.
    • You can switch to DEBUG CONSOLE in VS Code when the code is paused to enter debug commands.
0
source

Source: https://habr.com/ru/post/1247345/


All Articles