1

I have been trying to run python interactively from within sublime text 2.

I have tried what was suggested in this thread: 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 }) 

and using the key binding:

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

However, this works but gives me and error as I then can't get to my python modules.

This is the build I have running and this works fine, however it always exits before I can use interactive mode.

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

Thanks in advance for reading this!

Community
  • 1
  • 1

2 Answers2

0

Most likely python2.7 maps to OSX native Python installation by Apple, not Macports one.

Try changing it to:

    "cmd": ["/opt/local/bin/python2.7", "-ui", "$file"],
Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
0

Try the following.

Replace

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('run_existing_window_command', {
                        "id": "repl_python",
                        "file": "config/Python/Main.sublime-menu"
                    })

I borrowed the commands from the file /SublimeREPL/config/Python/Default.sublime-commands. I personally use IPython, so I use the option "id" : "repl_python_ipython".

Anyhow, the final plugin should like like so...

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

See if that works.

Danton Noriega
  • 686
  • 8
  • 12