Python Sublime Text 3 的构建系统问题 - 无法从正在运行的程序中获取输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19254765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:15:41  来源:igfitidea点击:

Issue with Sublime Text 3's build system - can't get input from running program

pythonwindows-7cmdsublimetext3

提问by Rook

I'm trying to get Sublime Text 3 (build 3049, if that matters) to run a Python script. A simple two liner

我正在尝试让 Sublime Text 3(构建 3049,如果重要的话)来运行 Python 脚本。一个简单的两个班轮

var = raw_input("Enter something: ")
print "You entered ", var

which asks for input, waits for it, then prints it out in windows console prompt.

它要求输入,等待它,然后在 Windows 控制台提示中打印出来。

Seeing the number of similar questions on the site, this is a problem for quite a number of users, so I went through those and tried ... stuff. Made a copy of exec.py file, commented that one line, made a new pythonw build file, tried messing about with the build file ... nothing seems to work.

看到网站上类似问题的数量,这对相当多的用户来说是一个问题,所以我浏览了这些并尝试了......东西。制作了 exec.py 文件的副本,评论了这一行,制作了一个新的 pythonw 构建文件,尝试弄乱构建文件......似乎没有任何效果。

In lack of a definite solution, how do you work with Python using Sublime Text 3?

在缺乏明确的解决方案的情况下,您如何使用 Sublime Text 3 使用 Python?

采纳答案by MattDMo

First off, since you're using a dev build, you must be a registered user (good!) and I'd recommend upgradingto 3053, the latest version, as newer is often better in terms of known issues being fixed. Second, just FYI, there's a fairly complete set of (unofficial) docs at docs.sublimetext.info. They're well-organized, pretty up-to-date, and cover a lot more ground than the "official" ones on sublimetext.com. Finally, in response to your comment, Sublime comes with a slightly stripped-down version of Python built-in. ST2 has 2.6, while ST3 has 3.3, so if you write plugins you'll need to conform to those language specifications. You can run arbitrary commandsfrom the console by hitting Ctrl`.

首先,由于您使用的是开发版本,因此您必须是注册用户(很好!),我建议升级到最新版本 3053,因为在修复已知问题方面,更新版本通常更好。其次,仅供参考,docs.sublimetext.info 上有一组相当完整的(非官方)文档。它们组织良好,是最新的,并且比 sublimetext.com 上的“官方”内容涵盖更多领域。最后,为了回应您的评论,Sublime 附带了一个稍微精简的 Python 内置版本。ST2 有 2.6,而 ST3​​ 有 3.3,因此如果您编写插件,您需要符合这些语言规范。您可以通过点击从控制台运行任意命令Ctrl`

As documented in several SO questions, Sublime Text on its own cannot handle input via raw_input()or input(). The same is true of other languages as well - Ruby's gets, Java's Scannerclass, Node's readlineclass, etc. The easiest short-term solution is to get Package Controlif you don't already have it, then install SublimeREPL. It allows you to transfer or run part or all of your code through the running REPL (you'll need to start one first).

正如几个 SO 问题中所记录的那样,Sublime Text 本身无法通过raw_input()或处理输入input()。其他语言也是如此——Ruby 的gets、Java 的Scanner类、Node 的readline类等。最简单的短期解决方案是获取Package Control(如果您还没有它),然后安装SublimeREPL。它允许您通过正在运行的 REPL 传输或运行部分或全部代码(您需要先启动一个)。

If the code you're running doesn't play well with SublimeREPL (for instance, you're using C/C++/Java/etc. and need to compile code before it runs), or you just want to run it independently of Sublime, you'll need to make your own build system. Save the following as Packages/User/Python_cmd.sublime-build:

如果您正在运行的代码不能很好地与 SublimeREPL 配合使用(例如,您正在使用 C/C++/Java/etc. 并且需要在运行之前编译代码),或者您只想独立于 Sublime 运行它,您需要制作自己的构建系统。将以下内容另存为Packages/User/Python_cmd.sublime-build

{
    "cmd": ["start", "cmd", "/k", "c:/python27/python.exe", "$file"],
    "selector": "source.python",
    "shell": true,
    "working_dir": "$file_dir"
}

changing the path to your Python executable as appropriate. Then, go to Tools -> Build Systemand select Python_cmd, and when you hit CtrlBto build, a new cmdwindow will open up with your file running. The /koption returns to the command prompt, without closing the window, after your program is done running so you can examine output, tracebacks, etc.

根据需要更改 Python 可执行文件的路径。然后,转到Tools -> Build System并选择Python_cmd,当您点击CtrlB构建时,cmd将打开一个新窗口,您的文件正在运行。/k在您的程序运行完成后,该选项将返回到命令提示符,而无需关闭窗口,以便您可以检查输出、回溯等。

回答by Shritam Kumar Mund

Sublime Text does not support inputting data into a program. For working with inputs you need to install a package called SublimeREPL. Follow this:

Sublime Text 不支持在程序中输入数据。要处理输入,您需要安装一个名为SublimeREPL的包。按照这个:

  1. open Sublime Text >> CTRL + P

  2. CTRL + P will open the Package control

  3. Click on Package Control: Install package
  4. Wait for a sec to pop up a search bar.
  5. Type SublimeREPL and Click it. It'll get installed in a few secs.
  1. 打开 Sublime Text >> CTRL + P

  2. CTRL + P 将打开包控件

  3. 点击包控制:安装包
  4. 稍等片刻,弹出搜索栏。
  5. 输入 SublimeREPL 并单击它。它会在几秒钟内安装。

Then follow the following steps to run your program;

然后按照以下步骤运行您的程序;

  1. Tools >> SublimeREPL >> Python >> Python run Current File
  1. 工具 >> SublimeREPL >> Python >> Python 运行当前文件

It'll open a new window, where you can give your input and get the output.

它将打开一个新窗口,您可以在其中提供输入并获得输出。