无法以Sublime Text将输入发送到正在运行的程序 [英] Can't send input to running program in Sublime Text

查看:95
本文介绍了无法以Sublime Text将输入发送到正在运行的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取Sublime Text 3来运行Python 2脚本.一个简单的两个班轮

I'm trying to get Sublime Text 3 to run a Python 2 script. A simple two liner

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

询问输入,等待输入,然后在Windows控制台提示符中将其打印出来.

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

看到网站上存在类似问题的数量,这对于很多用户来说是个问题,因此我仔细研究了这些问题并尝试了...东西.制作了exec.py文件的副本,评论说一行,制作了一个新的pythonw构建文件,试图弄乱该构建文件……似乎没有任何作用.

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.

在缺乏确定的解决方案的情况下,如何使用Sublime Text 3处理输入?

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

推荐答案

Sublime Text本身无法通过 raw_input()(Python 2)或 input()处理输入>(Python 3).其他语言也是如此-Ruby的 gets ,Java的 Scanner 类,Node的 readline 类, scanf 在C中使用 cin 在C ++中,等等.一种短期解决方案是获取包装控制如果尚未安装,请安装 SublimeREPL .它允许您通过正在运行的REPL传输或运行部分或全部代码.可能需要对 Main.sublime-menu 文件进行一些配置,才能使您首选的解释器正常运行.另外,您可以使用出色的 Terminus 插件-详细信息在底部.

Sublime Text on its own cannot handle input via raw_input() (Python 2) or input() (Python 3). The same is true of other languages as well - Ruby's gets, Java's Scanner class, Node's readline class, scanf in C, cin in C++, etc. One short-term solution is to get Package Control if 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. It may require some configuration of the Main.sublime-menu files to get your preferred interpreter to run properly. Alternatively, you can use the excellent Terminus plugin - details are at the bottom.

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

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:

{
    "cmd": ["start", "cmd", "/k", "c:/python38/python.exe", "$file"],
    "selector": "source.python",
    "shell": true,
    "working_dir": "$file_dir",
    "env": {"PYTHONIOENCODING": "utf-8"}
}

根据需要更改Python可执行文件的路径.然后,转到 Tools->构建系统 ,然后选择 Python_cmd ,然后在您按下 Ctrl B 进行构建时,将创建一个新的 cmd窗口将打开并运行您的文件./k 选项在程序运行完成后返回命令提示符,而无需关闭窗口,因此您可以检查输出,回溯等.

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

请注意,此构建系统特定于Windows,因为macOS和Linux没有 cmd .这些平台的构建系统如下.

Please note that this build system is Windows-specific, as macOS and Linux do not have cmd. Build systems for those platforms are below.

如果您正在运行OS X/macOS,则以下构建系统将在新的Terminal实例中打开程序.将其另存为 Packages/User/Python_Terminal.sublime-build .在macOS 10.15上进行的测试中,终端"窗口在激活时并不总是位于顶部,因此,如果您需要在其他窗口后寻找它.

If you are running OS X/macOS, the following build system will open your program in a new instance of Terminal. Save it as Packages/User/Python_Terminal.sublime-build. In my testing on macOS 10.15, the Terminal window didn't always come to the top when activated, so if you may need to look for it behind other windows.

{
    "shell_cmd": "osascript -e 'tell app \"Terminal\" to do script \"cd $file_path && python3 -u $file\"'",
    "working_dir": "$file_path",
    "selector": "source.python",
    "env": {"PYTHONIOENCODING": "utf-8"}
}

如果Python可执行文件的路径不在 $ PATH 上,则可能需要指定该路径.

You may need to specify the path to your Python executable if it's not on your $PATH.

最后,这是Linux的构建系统.它已经在Ubuntu上进行了测试,因此,如果您使用其他发行版,则需要确保已安装 gnome-terminal .将其另存为 Packages/User/Python_shell.sublime-build .程序运行完毕后,请按任意键以关闭窗口.

And finally, here is a build system for Linux. It was tested on Ubuntu, so if you use another distribution you'll need to ensure that gnome-terminal is installed. Save it as Packages/User/Python_shell.sublime-build. Once the program has finished running, hit any key to close the window.

{
    "shell_cmd": "gnome-terminal --working-directory=$file_path -- bash -c 'python3 -u \"$file\" && read -n 1 -s -r'",
    "working_dir": "$file_path",
    "selector": "source.python",
    "env": {"PYTHONIOENCODING": "utf-8"}
}


作为参考, Packages 目录是在选择 Preferences→Browse Packages ... 时打开的目录:


For reference, the Packages directory is the one opened when selecting Preferences → Browse Packages…:

  • Linux:〜/.config/sublime-text-3/Packages
  • OS X:〜/Library/Application Support/Sublime Text 3/Packages
  • Windows常规安装: C:\ Users \ 您的用户名 \ AppData \ Roaming \ Sublime Text 3 \ Packages
  • Windows可移植安装: InstallationFolder \ Sublime Text 3 \ Data \ Packages
  • Linux: ~/.config/sublime-text-3/Packages
  • OS X: ~/Library/Application Support/Sublime Text 3/Packages
  • Windows Regular Install: C:\Users\YourUserName\AppData\Roaming\Sublime Text 3\Packages
  • Windows Portable Install: InstallationFolder\Sublime Text 3\Data\Packages

我只用Python测试了这些构建系统,但是它们对于任何语言都应该可以正常工作.进行修改时,只需确保所有单引号和双引号都匹配-如果不匹配,则会出现错误或意外行为.

I have only tested these build systems with Python, but they should work fine for any language. When modifying, just make sure that all the single and double quotes match up – you'll get errors or unexpected behavior if they don't.

有一个独立于平台的插件,名为总站,该插件除其他功能外,还提供了代替默认的 exec 构建系统引擎.它允许您在代码下方的构建面板中与程序进行交互.从Package Control安装后,请创建以下构建系统(同样适用于Python):

There is a platform-independent plugin called Terminus that, among other things, provides a drop-in replacement for the default exec build system engine. It allows you to interact with your program in the build panel below your code. Once you've installed it from Package Control, create the following build system (again, for Python):

{
    "target": "terminus_exec",
    "cancel": "terminus_cancel_build",
    "cmd": [
        "/path/to/python", "-u", "$file"
    ],
    "working_dir": "$file_path",
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
}

您需要如上所述调整Python可执行文件的路径.请确保您已阅读文档,以找到可以使用此出色插件的所有其他方式.

You'll need to adjust the path to your Python executable, as above. Make sure you read the documentation to find out all the other ways you can make use of this great plugin.

这篇关于无法以Sublime Text将输入发送到正在运行的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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