我一直在尝试从sublime text 2中以交互方式运行python。
我已尝试过此帖子中的建议:Running Python interactively from within Sublime Text 2
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"}
然而,这有效,但是给了我和错误,因为我无法访问我的python模块。
这是我运行的构建,这很好用,但是在我可以使用交互模式之前总是会退出。
{
"cmd": ["python", "-ui", "$file"],
"path": "/opt/local/bin:/opt/local/sbin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
提前感谢您阅读本文!
答案 0 :(得分:0)
很可能python2.7
映射到Apple的OSX本机Python安装,而不是Macports安装。
尝试将其更改为:
"cmd": ["/opt/local/bin/python2.7", "-ui", "$file"],
答案 1 :(得分:0)
请尝试以下操作。
替换
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('run_existing_window_command', {
"id": "repl_python",
"file": "config/Python/Main.sublime-menu"
})
我借用了文件/SublimeREPL/config/Python/Default.sublime-commands
中的命令。我个人使用IPython,所以我使用选项"id" : "repl_python_ipython"
。
无论如何,最终的插件应该是这样......
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",
"file": "config/Python/Main.sublime-menu"
})
self.window.run_command('move_to_group', { "group": 1 })
看看是否有效。