Running python script in Visual Studio code; how to make `input ()` work?


I'm trying to get simple input using input() , but when I run the script in Visual Code, the program freezes whenever this line of code hits.

  • How to run code in Visual Studio code and use input() ?

<sub> task sub>

 { "version": "0.1.0", "command": "python", "isShellCommand": true, "showOutput": "always", "args": ["${file}"], } 
+5
source share
3 answers

Introduction

You will need to run the script from the command line (terminal) instead of direct Visual Studio code if you want to interact with the program as a regular user.

 > python name_of_program.py 

Development

The output displayed inside Visual Studio code is not intended to interact with the base script, and it does not have the ability to read any data directly from your keyboard (it simply shows the output of what you decided to run).


Bypass

What you can do is edit your task file to automatically create the selected terminal, and not directly run python -interpreter.

Depending on which operating system you are in and the available terminals, the changes necessary for this may look a little different, but they should all follow the same pattern.

 { "version": "0.1.0", "command": "urxvt", "isShellCommand": false, "showOutput": "always", "args": [ "-e", "python ${file}" ] } 

NOTE
In the above example, urxvt is the name of my choice for the terminal, -e is the flag needed to pass the command that should be executed at startup, and python ${file} is the command to execute.

My recommendation is to get the command necessary to launch a new terminal, and directly execute the python script, working elsewhere, before editing the problem file.

+6
source

You can right-click in the text file that you want to run, and then select "Run Python file in terminal."

+6
source

I had a similar problem and I assume that you started the program with

ctrl + shift + B to build.

instead of building, you can just open the terminal inside the vs code

ctrl + shift + `

After opening the terminal, enter the name of the file you want to run.

+1
source

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


All Articles