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

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

问题描述

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

I'm trying to get Sublime Text 3 to run a Python 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 处理输入?

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

推荐答案

Sublime Text 本身无法通过 raw_input() (Python 2) 或 input() 处理输入>(Python 3).其他语言也是如此——Ruby 的gets、Java 的Scanner 类、Node 的readline 类、scanf在 C 中,cin 在 C++ 中,等等.一种短期解决方案是获得 Package Control如果您还没有,请安装 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/等,并且需要在运行之前编译代码),或者您只想运行它独立于 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 ->Build System 并选择Python_cmd,当你点击CtrlB 构建时,一个新的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,以下构建系统将在新的终端实例中打开您的程序.将其另存为 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~/.config/sublime-text/Packages
  • OS X:~/Library/Application Support/Sublime Text 3/Packages~/Library/Application Support/Sublime Text/Packages
  • Windows 常规安装:C:UsersYourUserNameAppDataRoamingSublime Text 3PackagesC:Users你的用户名AppDataRoamingSublime TextPackages
  • Windows 便携式安装:InstallationFolderSublime Text 3DataPackages InstallationFolderSublime TextDataPackages
  • Linux: ~/.config/sublime-text-3/Packages or ~/.config/sublime-text/Packages
  • OS X: ~/Library/Application Support/Sublime Text 3/Packages or ~/Library/Application Support/Sublime Text/Packages
  • Windows Regular Install: C:UsersYourUserNameAppDataRoamingSublime Text 3Packages or C:UsersYourUserNameAppDataRoamingSublime TextPackages
  • Windows Portable Install: InstallationFolderSublime Text 3DataPackages InstallationFolderSublime TextDataPackages

确切路径取决于版本以及您是否从 Sublime Text 3 升级.

我只用 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.

有一个名为 Terminus 的独立于平台的插件,除其他外,它提供了一个 drop-代替默认的 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天全站免登陆