Running Python interactively from within Sublime Text 2

17,972

Solution 1

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.

Solution 2

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"
                    } 
    }
]
Share:
17,972

Related videos on Youtube

yegodz
Author by

yegodz

Updated on September 15, 2022

Comments

  • yegodz
    yegodz over 1 year

    I have looked at all the answers on this forum but I'm missing something. I want to be able to hit Cmd+B while editing a Python file "myfile.py" in Sublime Text 2.

    This should open up a Python shell that loads my file and returns me to the interactive prompt so the namespace in my Python script is available.

    Setting the -i option in the build setting still closes the interpreter (see below)

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

    I downloaded sublimeREPL but I'm not sure how to set the -i option.
    Any help is appreciated

  • yegodz
    yegodz over 11 years
    Thanks! this is exactly what I needed!! Is it possible to open the repl terminal as a split window horizontally or as a an external window?
  • sneawo
    sneawo over 11 years
    I think it's possible with macro stackoverflow.com/questions/9646552/…
  • dbn
    dbn over 11 years
    Wouldn't this make more sense as a build rule?
  • minerals
    minerals almost 11 years
    this does not work for windows, getting error system cannot find the file specified. Can you please tip what should be changed?
  • dbn
    dbn almost 11 years
    This looks exactly like the default build rule. Do you think that this does something different from the default build rule?
  • Doktoro Reichard
    Doktoro Reichard almost 10 years
    A note: this may break when trying to access dictionaries. Adding "extend_env": {"PYTHONIOENCODING": "utf-8"} to the command arguments fixes this issue.
  • Matt Bannert
    Matt Bannert over 9 years
    Just wonder if it's possible like with REPL:R to send stuff from a script to an already existing REPL console.
  • hhh
    hhh over 9 years
    Great! Is it possible to run the file in a REPL window already open ?