【问题标题】:Running Python interactively from within Sublime Text 2在 Sublime Text 2 中以交互方式运行 Python
【发布时间】:2012-12-14 08:10:17
【问题描述】:

我已经查看了这个论坛上的所有答案,但我遗漏了一些东西。 我希望能够在 Sublime Text 2 中编辑 Python 文件“myfile.py”时点击 Cmd+B

这应该会打开一个 Python shell,它会加载我的文件并将我返回到交互式提示,以便我的 Python 脚本中的命名空间可用。

在构建设置中设置-i 选项仍会关闭解释器(见下文)

> 81
> >>>  [Finished in 0.1s]

我下载了 sublimeREPL,但不知道如何设置 -i 选项。
任何帮助表示赞赏

【问题讨论】:

    标签: python sublimetext2 sublimerepl


    【解决方案1】:

    我想快速编辑@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!

    【讨论】:

      【解决方案2】:

      好的,感谢 sneawo 的提示!这是我第一次这样做。

      步骤 1. 创建一个插件 pydev,(从 Tools->New Plugin)创建一个命令 '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-horizo​​ntal。可能会修饰代码以进行一些检查,并简单地在现有布局中添加一个水平窗口。关闭 repl 选项卡时删除水平缓冲区也很好。

      【讨论】:

      • 这不适用于 Windows,出现错误 system cannot find the file specified。你能告诉我应该改变什么吗?
      • 注意:这可能会在尝试访问字典时中断。将"extend_env": {"PYTHONIOENCODING": "utf-8"} 添加到命令参数可解决此问题。
      • 只是想知道是否可以像 REPL:R 一样将脚本中的内容发送到现有的 REPL 控制台。
      【解决方案3】:

      答案比你的方法简单得多。 只需定义一个新的构建“配置文件”(构建系统),在其中您可以准确捕获默认的 Python 构建,除了将选项 -u 更改为 -ui

      { "cmd": ["C:\\python33\\python.exe", "-ui", "$file"], "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)", "selector": "source.python" }

      【讨论】:

        【解决方案4】:

        这是一种创建新构建系统的简单方法。

        {
            "cmd": ["C:\\python33\\python.exe", "-u", "$file"],
            "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
            "selector": "source.python"
        }
        

        然后将构建系统保存为 Python3 或 Python27 并选择它作为默认值。

        【讨论】:

        • 这看起来与默认的构建规则完全一样。你认为这与默认的构建规则有什么不同吗?
        【解决方案5】:

        尝试更新您的用户键绑定:

        [
            { "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"
                            } 
            }
        ]
        

        【讨论】:

        • 谢谢!这正是我需要的!是否可以将repl终端作为水平拆分窗口或外部窗口打开?
        • 我认为可以使用宏stackoverflow.com/questions/9646552/…
        • 这不是更有意义的构建规则吗?
        • 太棒了!是否可以在已打开的 REPL 窗口中运行该文件?
        猜你喜欢
        • 2014-02-05
        • 2017-02-07
        • 1970-01-01
        • 2016-07-12
        • 2014-04-23
        • 2014-05-28
        • 2014-08-05
        • 1970-01-01
        • 2016-02-22
        相关资源
        最近更新 更多