简体   繁体   English

在 Visual Studio Code 中运行 python 脚本; 如何让 `input ()` 工作?

[英]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.我正在尝试使用input()获取一个简单的输入,但是当我在Visual Code中运行脚本时,只要命中该行代码,程序就会挂起。

  • How can I run code within Visual Studio Code and use input() ?如何在Visual Studio Code中运行代码并使用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.如果您想像普通用户一样与程序交互,则需要从命令行(终端)运行脚本,而不是直接在Visual Studio Code 中运行

> 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). Visual Studio Code 中显示的输出并不打算用于与底层脚本交互,也不能直接从键盘读取任何输入(它只是显示您决定运行的任何内容的输出)。


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.您可以做的是编辑您的任务文件以自动生成您选择的终端,而不是直接运行python -interpreter。

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.在上面, urxvt是我选择的终端名称, -e是传递要在启动时执行的命令所需的标志, python ${file}是要执行的命令。

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.我的建议是获取启动新终端所需的命令,并直接执行 python 脚本,在编辑任务文件之前在其他地方工作

I had similar problem and I guess you ran the program with我有类似的问题,我猜你用

ctrl + shift + B for build. ctrl + shift + B 用于构建。

instead of build, you can simply open up terminal inside of vs code by而不是构建,您可以简单地在 vs 代码中打开终端

ctrl + shift + ` 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. 首先,使用vscode打开一个文件夹,然后将xx.py文件拖放到该文件夹​​中,最后使用终端命令:pythonxx.py。

just click on the RUN CODE option at the top right corner of visual studio ode interference.只需点击visual studio ode interference右上角的RUN CODE选项即可。 When you run code it automatically runs (input()) function in Terminal.当您运行代码时,它会在终端中自动运行 (input()) function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Visual Studio (VS) 代码上获得 Intellisense 以在 Python 上工作 - How to get Intellisense on Visual Studio (VS) Code to work on Python 为什么在 Visual Studio Code 上运行 Python 3 脚本时会出现 SyntaxError? - Why do I get SyntaxError when running a Python 3 script on Visual Studio Code? 如何在Visual Studio Code上执行Python脚本 - How to execute a Python script on Visual Studio Code Visual Studio Code 终端在 Powershell 中继续运行 Python 脚本 - Visual Studio Code Terminal keeps running Python script in Powershell Python计划脚本在IDLE中运行,但不在Visual Studio Code中运行 - Python schedule script running in IDLE but not in Visual Studio Code 无法让 python 在 Visual Studio 代码上正常工作 - Cant get python to properly work on visual studio code 在Visual Studio Code中运行Python代码? - Running Python code inside Visual Studio Code? 在 Visual Studio 代码中运行 python 代码? - Running python code in Visual Studio code? 如何获取 Python 脚本以在 Adobe Illustrator 和 Visual Studio Code 中绘制简单的线条 - How do I get Python Script to draw a simple line in Adobe Illustrator and Visual Studio Code 在 Microsoft Visual Studio 中使用 arguments 运行 python 脚本 - Running python script with arguments in microsoft visual studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM