在Sublime Text 2中以交互方式运行Python
我已经看了这个论坛上的所有答案,但我遗漏了一些东西。 我希望能够在Sublime Text 2中编辑Python文件“myfile.py”时点击Cmd+B 这将打开一个pythonshell,加载我的文件并将我返回到交互式提示符,以便Python脚本中的名称空间可用 在构建设置中设置在Sublime Text 2中以交互方式运行Python,python,sublimetext2,sublimerepl,Python,Sublimetext2,Sublimerepl,我已经看了这个论坛上的所有答案,但我遗漏了一些东西。 我希望能够在Sublime Text 2中编辑Python文件“myfile.py”时点击Cmd+B 这将打开一个pythonshell,加载我的文件并将我返回到交互式提示符,以便Python脚本中的名称空间可用 在构建设置中设置-i选项仍然会关闭解释器(见下文) 我下载了submiterepl,但我不确定如何设置-I选项。 非常感谢您的帮助请尝试更新您的用户密钥绑定: [ { "keys": ["super+shift+r"], "
-i
选项仍然会关闭解释器(见下文)
我下载了submiterepl,但我不确定如何设置-I
选项。非常感谢您的帮助请尝试更新您的用户密钥绑定:
[
{ "keys": ["super+shift+r"], "command": "repl_open",
"caption": "Python",
"mnemonic": "p",
"args": {
"type": "subprocess",
"encoding": "utf8",
"cmd": ["python", "-i", "-u", "$file"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python"
}
}
]
好的,谢谢sneawo的提示!这是我做这件事的第一个切入点 第一步。创建一个插件pydev,(从工具->新插件),该插件创建一个命令“pydev”
import sublime, sublime_plugin
class PydevCommand(sublime_plugin.WindowCommand):
def run(self):
self.window.run_command('set_layout', {"cols":[0.0, 1.0], "rows":[0.0, 0.5, 1.0], "cells":[[0, 0, 1, 1], [0, 1, 1, 2]]})
self.window.run_command('repl_open',{"type": "subprocess",
"encoding": "utf8",
"cmd": ["python2.7", "-i", "-u", "$file"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python2.7"
})
self.window.run_command('move_to_group', { "group": 1 })
第二步。在“首选项->密钥绑定”用户界面中创建新的密钥绑定
{“keys”:[“f5”],“command”:“pydev”}
现在按f5键(在Mac上,默认情况下是fn+f5)可以启动repl选项卡中的python解释器,将布局设置为水平两个窗口,并将repl选项卡移动到较低的窗口
这是非常基本的,因为它不检查当前布局是什么,只是将布局设置为2水平。可能会对代码进行一些检查,并简单地向现有布局添加一个水平窗口。当repl选项卡关闭时,删除水平缓冲区也很好。这里是一个创建新构建系统的简单方法
{
"cmd": ["C:\\python33\\python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
然后将构建系统另存为Python3或Python27,并将其选择为默认值 答案比你的方法简单得多。 只需定义一个新的构建“概要文件”(构建系统),在其中您可以准确地捕获默认的Python构建,除了将选项
-u
更改为-ui
{
“cmd”:[“C:\\python33\\python.exe”、“-ui”、“$file”],
“文件正则表达式”:“^[]*文件\”(…*?)\”,第([0-9]*)行,
“选择器”:“source.python”
}
我想在@user1936097的答案中添加一个快速编辑 我复制了这个想法,但想改为加载IPython(原样的代码可以很好地加载标准Python)。我替换了
self.window.run_command('repl_open',{"type": "subprocess",
"encoding": "utf8",
"cmd": ["python2.7", "-i", "-u", "$file"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python2.7"
})
与
self.window.run_command('repl_open', {
"type": "subprocess",
"encoding": "utf8",
"autocomplete_server": true,
"cmd": ["python","-u","${packages}/SublimeREPL/config/Python/ipy_repl.py"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python",
"extend_env": {
"PYTHONIOENCODING": "utf-8",
"SUBLIMEREPL_EDITOR": "$editor"}
})
但它不起作用
行“自动完成服务器”:true似乎是问题所在。如果我删除了它,代码就会运行,但是IPython打开了关闭的。如果我离开了,什么也没发生
最后,我借用了文件/SublimeREPL/config/Python/Default.sublime commands
中的一个命令,并提出了
self.window.run_command('run_existing_window_command', {
"id": "repl_python_ipython",
"file": "config/Python/Main.sublime-menu"
})
这使得最终的插件代码:
import sublime, sublime_plugin
class PydevCommand(sublime_plugin.WindowCommand):
def run(self):
self.window.run_command('set_layout', {"cols":[0.0, 1.0], "rows":[0.0, 0.5, 1.0], "cells":[[0, 0, 1, 1], [0, 1, 1, 2]]})
self.window.run_command('run_existing_window_command', {
"id": "repl_python_ipython",
"file": "config/Python/Main.sublime-menu"
})
self.window.run_command('move_to_group', { "group": 1 })
将其分配给一个键绑定,如原始帖子中所示,您现在将加载IPython而不是标准Python 谢谢!这正是我需要的!!可以将repl终端作为水平拆分窗口或外部窗口打开吗?我认为可以使用宏打开。作为构建规则,这不是更有意义吗?太好了!是否可以在已打开的REPL窗口中运行该文件?这不适用于windows,获取错误
系统无法找到指定的文件。您能告诉我应该更改什么吗?注意:当尝试访问字典时,这可能会中断。在命令参数中添加“extend_env”:{“PYTHONIOENCODING”:“utf-8”}
可以解决此问题。只想知道是否可以像REPL:R
那样将脚本中的内容发送到现有的REPL控制台。这与默认生成规则完全相同。您认为这与默认构建规则有什么不同吗?
import sublime, sublime_plugin
class PydevCommand(sublime_plugin.WindowCommand):
def run(self):
self.window.run_command('set_layout', {"cols":[0.0, 1.0], "rows":[0.0, 0.5, 1.0], "cells":[[0, 0, 1, 1], [0, 1, 1, 2]]})
self.window.run_command('run_existing_window_command', {
"id": "repl_python_ipython",
"file": "config/Python/Main.sublime-menu"
})
self.window.run_command('move_to_group', { "group": 1 })