1

我一直在尝试从 sublime text 2 中以交互方式运行 python。

我已经尝试了这个线程中的建议:从 Sublime Text 2 中以交互方式运行 Python

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"
}

提前感谢您阅读本文!

4

2 回答 2

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 })

看看这是否有效。

于 2014-12-23T12:04:02.763 回答
0

最有可能python2.7映射到 Apple 的 OSX 本机 Python 安装,而不是 Macports 之一。

尝试将其更改为:

    "cmd": ["/opt/local/bin/python2.7", "-ui", "$file"],
于 2014-01-20T07:55:06.990 回答