在 Visual Studio Code 中运行 python 脚本;如何让`input()`工作? [英] Running python script in Visual Studio Code; how to get `input ()` to work?

查看:39
本文介绍了在 Visual Studio Code 中运行 python 脚本;如何让`input()`工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试使用 input() 获取一个简单的 input,但是当我在 Visual Code 中运行脚本时,该程序在该行时挂起代码被命中.

  • 如何在 Visual Studio Code 中运行代码并使用 input()?
<小时>

任务

<代码>{版本":0.1.0",命令":蟒蛇",isShellCommand":真,"showOutput": "总是","args": ["${file}"],}

解决方案

简介

如果您想与程序交互,则需要从 命令行(终端)运行脚本,而不是直接在 Visual Studio Code 中运行就像普通用户一样.

<代码>>python name_of_program.py


细化

显示在 Visual Studio Code 中的输出不是用来与底层脚本交互的,也不能直接从键盘读取任何输入(它只是显示输出无论你决定运行什么).


解决方法

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

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

<代码>{版本":0.1.0","command": "urxvt",isShellCommand":假,"showOutput": "总是","args": [ "-e", "python ${file}" ]}

<块引用>

注意
在上面,urxvt是我选择的终端名称,-e 是传递启动时要执行的命令所需的标志,python ${file} 是要执行的命令.

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


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

解决方案

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

这篇关于在 Visual Studio Code 中运行 python 脚本;如何让`input()`工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆