Running Python interactively from within Sublime Text 2

别来无恙 提交于 2019-11-27 19:30:29
user1936097

ok, thanks to sneawo for the hints! Here's my first cut at doing this.

Step 1. Create a plugin pydev, (from Tools->New Plugin) which creates a command '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 }) 

Step 2. Create a new key binding in the Preferences->Key-Bindings-user

{"keys": ["f5"], "command": "pydev"}

Now pressing f5 (on the Mac it will be fn+f5 by default) does the trick-it will start the python interpreter in a repl tab, set the layout to two-window horizontal and move the repl tab to the lower window.

This is pretty basic in that it doesn't check to see what the current layout is and simply sets the layout to 2-horizontal. Will probably spruce up the code to do some checking and simply add a horizontal window to the existing layout. Also would be good to remove the horizontal buffer when the repl tab is closed.

Try to update your user keybindings:

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

The answer is much more simple than your approach. Just define a new build "profile" (Build system), in it you capture exactly the default Python build, except changing option -u to -ui

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

I wanted to add a quick edit to the answer by @user1936097.

I copied the idea but wanted to instead load IPython (the code as is works fine to load standard Python). I replaced...

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

with...

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

But it didn't work.

The line "autocomplete_server": true seemed to be the problem. If I removed it, the code ran, but IPython opened the closed. If I left it, nothing happened.

I finally borrowed a command found in the file /SublimeREPL/config/Python/Default.sublime-commands and came up with...

self.window.run_command('run_existing_window_command', {
                        "id": "repl_python_ipython",
                        "file": "config/Python/Main.sublime-menu"
                    })

This made the final plugin code:

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

Assign this to a keybind as shown in the original post and you will now load IPython instead of standard Python!

Here is an easy way just creating a new build system.

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

Then save the build system as Python3 or Python27 and select it as default.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!