Running python script in Visual Studio Code; how to get `input ()` to work?
Solution 1
Introduction
You will need to run your script from the command-line (terminal), instead of directly in Visual Studio Code, if you would like to interact with the program as a normal user would.
> python name_of_program.py
Elaboration
The output displayed inside Visual Studio Code is not meant to be used for interacting with the underlying script, nor does it have the capability to read any input directly from your keyboard (it simply shows the output of whatever you have decided to run).
Workaround
What you could do is to edit your task-file to automatically spawn a terminal of your choosing instead of running the python
-interpreter directly.
Depending on what operating system you are on, and the terminals available, the edits required to do this might 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}" ]
}
N O T E
In the above,urxvt
is the name of my choice for terminal,-e
is the flag required to pass a command that is to be executed upon startup, andpython ${file}
is the command to execute.
My recommendation is to get the command necessary to fire up a new terminal, and directly execute a python script, working elsewhere before editing your task-file.
Solution 2
You can right click within the text file that you wish to run and then select "Run Python file in Terminal".
Solution 3
I had similar problem and I guess you ran the program with
ctrl + shift + B for build.
instead of build, you can simply open up terminal inside of vs code by
ctrl + shift + `
once terminal is opened, type in the name of file you'd like to run.
Related videos on Youtube
Andrea Tulimiero
My name is Andrea and I’m a Computer Science student at ETH University of Zurich. I deal with advanced computer programming but I delight myself with CTFs and UX/UI as well I’ve worked as a freelance full-stack web developer for years, using Django and Vue.js. As a hobbist I’ve also built mobile apps for Android, some of them as clients for IoT projects involving Arduino I truly believe in the power of Open Source, and when I’m able to, I love contributing to open source projects. I’m quite active on Stack Overflow as well
Updated on September 15, 2022Comments
-
Andrea Tulimiero over 1 year
I'm trying to grab a simple input usinginput()
, but when I run the script within Visual Code the program hangs whenever that line of code is hit.- How can I run code within Visual Studio Code and use
input()
?
task
{ "version": "0.1.0", "command": "python", "isShellCommand": true, "showOutput": "always", "args": ["${file}"], }
- How can I run code within Visual Studio Code and use
-
Andrea Tulimiero over 8 yearsThank you very much ! @Filip Although if it would have been better to run right in VS Code, I think this the best workaround !
-
Filip Roséen - refp over 8 yearsDo not forget to upvote the answer, and marking it as accepted, to flag this question as "solved"! Welcome to stackoverflow, @AndreaTulimiero!
-
Andrea Tulimiero over 8 yearsI'd like to but I don't have enough repo ! @Filip
-
Andrea Tulimiero over 8 yearsHowever I marked it as the best answer Thx again Man ! @Filip
-
Filip Roséen - refp over 8 years@AndreaTulimiero now you do! ;)
-
Justyn over 7 yearsThis appears to be the most straight forward and correct answer.
-
Piotr Tobolski over 7 yearsDo you know how to create keyboard shortcut for this option?
-
Ankur about 7 years@Tobol
[ { "key": "cmd+r", "command": "python.execInTerminal" } ]
-
PatrickT about 6 yearsAnother good one is
[ { "key": "cmd+r", "command": "python.execSelectionInTerminal" } ]