简体   繁体   中英

Running python script in Visual Studio Code; how to get `input ()` to work?


I'm trying to grab a simple input using input() , 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}"],
}

您可以右键单击要运行的文本文件,然后选择“在终端中运行 Python 文件”。

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.

> 

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}" ]  
}

NOTE
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, and python ${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.

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.

I have the same problem with you, I am using the following methods to solve.

First, open a folder with vscode, then drag and drop the xx.py file into the folder, and finally use the terminal command: python xx.py.

just click on the RUN CODE option at the top right corner of visual studio ode interference. When you run code it automatically runs (input()) function in Terminal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM