我看过这个论坛上的所有答案,但是似乎还缺少一些信息。我想在Sublime Text 2编辑Python文件“myfile.py”时,能够按下Cmd+B。
这样应该会打开一个Python shell,加载我的文件并返回到交互提示符,以便我可以使用Python脚本中的命名空间。
在构建设置中设置-i
选项仍然会关闭解释器(见下文)。
> 81
> >>> [Finished in 0.1s]
我下载了 sublimeREPL,但是不确定如何设置 -i
选项。
谢谢任何帮助。
我看过这个论坛上的所有答案,但是似乎还缺少一些信息。我想在Sublime Text 2编辑Python文件“myfile.py”时,能够按下Cmd+B。
这样应该会打开一个Python shell,加载我的文件并返回到交互提示符,以便我可以使用Python脚本中的命名空间。
在构建设置中设置-i
选项仍然会关闭解释器(见下文)。
> 81
> >>> [Finished in 0.1s]
我下载了 sublimeREPL,但是不确定如何设置 -i
选项。
谢谢任何帮助。
好的,感谢sneawo的提示!这是我第一步尝试。
步骤1. 创建一个pydev插件(从Tools->新建插件),它创建一个名为“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 })
步骤2. 在“Preferences->Key-Bindings-user”中创建一个新的键绑定
{"keys": ["f5"], "command": "pydev"}
现在按下f5(在Mac上默认为fn+f5),它将启动Python解释器,并在一个repl标签页中设置布局为两个水平窗口,并将repl标签移动到较低的窗口。
这非常基础,因为它不检查当前的布局并简单地将布局设置为2-horizontal。可能会优化代码以进行一些检查并将水平窗口添加到现有布局中。当repl标签关闭时,还可以删除水平缓冲区。
尝试更新用户按键绑定:
[
{ "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"
}
}
]
我想对@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"}
})
但它没有起作用。
这行代码"autocomplete_server": 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!
答案比你的方法简单得多。
只需定义一个新的构建“配置文件”(构建系统),在其中捕获默认的Python构建,除了将选项-u
更改为-ui
{ "cmd": ["C:\\python33\\python.exe", "-ui", "$file"], "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)", "selector": "source.python" }
这里有一个简单的方法,只需创建一个新的构建系统。
{
"cmd": ["C:\\python33\\python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
"extend_env": {"PYTHONIOENCODING": "utf-8"}
添加到命令参数中可以解决此问题。 - Doktoro ReichardREPL:R
一样,将脚本中的内容发送到已经存在的 REPL 控制台。 - Matt Bannert