【问题标题】:Sublime Text 2 and interactive Macports Python 2.7 sessionSublime Text 2 和交互式 Macports Python 2.7 会话
【发布时间】:2014-02-05 13:06:16
【问题描述】:

我一直在尝试从 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"
}

提前感谢您阅读本文!

【问题讨论】:

    标签: python macos sublimetext2 sublimerepl


    【解决方案1】:

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

    尝试将其更改为:

        "cmd": ["/opt/local/bin/python2.7", "-ui", "$file"],
    

    【讨论】:

      【解决方案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('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 })
      

      看看这是否有效。

      【讨论】:

        猜你喜欢
        • 2012-12-14
        • 2013-02-21
        • 2016-07-12
        • 1970-01-01
        • 1970-01-01
        • 2012-10-18
        • 2012-02-26
        • 2023-03-06
        • 2012-07-22
        相关资源
        最近更新 更多