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

2024-04-23 17:22:07 发布

您现在位置:Python中文网/ 问答频道 /正文


我试图使用input()获取一个简单的输入,但是当我在可视化代码中运行脚本时,只要碰到这行代码,程序就会挂起。

  • 如何在Visual Studio代码中运行代码并使用input()

任务

{
    "version": "0.1.0",

    "command": "python",

    "isShellCommand": true,

    "showOutput": "always",

    "args": ["${file}"],
}

Tags: 代码程序脚本trueinputversion可视化args
3条回答

我也有类似的问题,我想你运行程序时

ctrl+shift+B表示构建。

不用构建,您只需通过

ctrl+shift+`

打开终端后,键入要运行的文件名。

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

简介

如果希望像普通用户那样与程序交互,则需要从命令行(终端)运行脚本,而不是直接在Visual Studio代码中运行脚本。

> python name_of_program.py

精化

Visual Studio代码中显示的输出不用于与底层脚本交互,也不具有直接从键盘读取任何输入的能力(它只显示您决定运行的任何脚本的输出)。


解决办法

您可以做的是编辑任务文件,以自动生成您选择的终端,而不是直接运行python解释器。

根据您所使用的操作系统和可用的终端,执行此操作所需的编辑可能看起来有些不同,但它们都应遵循相同的模式。

{
           "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, 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.

相关问题 更多 >