40

I really like using sublime text 2 to write Python codes, however any time I try to run a script which has an input, the sublime text console reports an error. So, I decided to try SublimeREPL, however I've been searching for hours and I didn't find out how to run Python code... could you guys help me?

I want to run the code on SublimeREPL as we do with the sublime text console (CTRL+b).. what I actually want to know is whether or not there's a way to do the same with SublimeREPL.

Thank you in advance!

k-dubs
  • 29
  • 7
wombatp
  • 813
  • 4
  • 10
  • 14
  • 1
    Did you read the [documentation](http://sublimerepl.readthedocs.org/en/latest/)? What exactly are you having a problem with? Please [edit](http://stackoverflow.com/posts/19732006/edit) your question and describe exactly what you want to do, what you've tried to do so far, and the specific error(s), if any, you are getting. – MattDMo Nov 01 '13 at 17:35
  • uhm, I suppose I wasn't clear enough.. I wan to run the code on repl as we do with the sublime text console (crtl+b).. what I actually want to know is whether there's a way to do the same with repl or not, 'cause I couldn't find any answer yet – wombatp Nov 01 '13 at 18:03

4 Answers4

78

As described here, create a new Build System file and save it as ..\Packages\User\SublimeREPL-python.sublime-build. The file should contain:

{
    "target": "run_existing_window_command", 
    "id": "repl_python_run",
    "file": "config/Python/Main.sublime-menu"
}

Then go to your Python file tab and select Tools > Build System > SublimeREPL-python. Now, Ctrl + B should execute the current Python file, with the output in a new tab. If you use a two column layout, the REPL output should open in the second column. (This was using Sublime Text 3.)

sblair
  • 1,085
  • 2
  • 13
  • 22
  • 1
    This is absolutely perfect and worked first time. I don't understand why SublimeREPL doesn't do this out of the box. Thank you. – SleepyCal May 27 '14 at 00:31
  • 5
    @sblair This is great. Two questions: (1) Is there a way to make it so that if a REPL tab is already open, it will simply reload in that tab (instead of opening a new one)? (2) Is there a way to use `Ctrl + B` and then leave REPL open so that it remains in interactive Python console? – Alex P. Miller Jul 10 '14 at 20:38
  • @alexpmil Sorry, I don't know. But now that you mention it, both of those would be useful! Probably best to ask on the forum, but please leave an answer here if you find out how - I would upvote it! – sblair Jul 11 '14 at 01:02
  • @sblair, thanks, I use this to prevent the REPL from closing when `sys.exit()` is called. – Stephen Niedzielski Jul 24 '14 at 03:35
  • 1
    This is the answer that should be accepted, works perfectly. Thank you! – Gabriel May 07 '15 at 12:10
  • 2
    @AlexP.Miller For 2): you can get an interactive Python console by editing the entry for `repl_python_run` in `Packages/SublimeREPL/config/Python/Main.sublime-menu`. Including the flag `-i` in the `cmd`. Got this from: https://www.youtube.com/watch?v=wM2LbXCkLDI – fabern Dec 19 '19 at 12:28
30

First "Install Package Control" from https://sublime.wbond.net/installation#st2

Optional(To check the above package is successfully installed: Click the Preferences > Browse Packages… at this folder Click Back Button one time and then into the Installed Packages/ folder, check there will be Package Control.sublime-package file)

then go to Preferences > Package Control > Package Control: Install Package in sublime text 2

find SublimeREPL in list.

Restart SublimeText2

open Preferences > Package Settings > SublimeREPL > Settings - Default file copy all text from there.

then open Preferences > Package Settings > SublimeREPL > Settings - User and paste the text here.

Restart SublimeText2

Go to Tools > SublimeREPL > Python > Python

And you are done

Nishant Bhakta
  • 2,897
  • 3
  • 21
  • 24
  • 1
    thanks for your answer, but I'm all set up already. The point is that I didn't find a way to run the code I wrote on repl, like we do normally with the sublime text console (ctrl+b).. – wombatp Nov 01 '13 at 18:06
  • 3
    Yes, simply write and save your code on Sublime text2 editor. And after save go to `Tools > SublimeREPL > Python > Python-RUN Current file` – Nishant Bhakta Nov 01 '13 at 18:27
  • 3
    thanks for your answer! it was very helpfull! btw, there's a shortcut for that command? – wombatp Nov 01 '13 at 18:30
  • 7
    @user205820 yes, there is a shortcut, and there are several ways to run/transfer code in the REPL. First, open the REPL you want to use to evaluate your code, switch back to the tab with your code, and hit `Ctrl+,`, `f` to transfer the file to the REPL. Other commands are available by going to `Tools -> SublimeREPL -> Eval in REPL` and `Transfer to REPL`. – MattDMo Nov 01 '13 at 21:26
  • Is this the same when I'm coding in Ruby or something like that ? – TheBAST Jun 26 '19 at 13:26
  • Whats the solution for Sublime Text 3 ? does sublimeREPL works for ST3 too ? or if not whats the solution. – indianwebdevil Apr 28 '21 at 15:48
26

Steps to make Sublime Python Console which is Interactive and Reusable :

1) Install SublimeREPL plugin :

In Top Bar > "Tools" > "Command Palette" > "Package Control: Install Package"
Search for : "SublimeREPL" and install


2) Create Build System :

In Top Bar > "Tools" > "Build System" > "New Build System"

Replace all contents of the file with :

{
    "target": "run_existing_window_command", 
    "id": "repl_python_run",
    "file": "config/Python/Main.sublime-menu"
}

Save the file as "PythonRepl.sublime-build" in the default "user" folder.


3) Settings to make Console interactive and Reusable:

|=> Goto "Preferences" > "Browse Packages"

|=> Goto Folder : SublimeRepl

|=> Edit : sublimerepl.py

Replace : if view.id() == view_id

With    : if view.name() == view_id:

|=> Goto Folder : SublimeRepl/config/Python

|=> Edit : Main.sublime-menu

|=> Under "caption": "Python - RUN current file"

|=> Append : "-i", in "cmd" as : 

        "cmd": ["python", "-u", "$file_basename"],

        "cmd": ["python", "-i", "-u", "$file_basename"],

|=> Add : Before "external_id": "python"

        "view_id": "*REPL* [python]",

|=> Full Code as shown below :
    --------------------------------------------------
    {"command": "repl_open",
     "caption": "Python - RUN current file",
     "id": "repl_python_run",
     "mnemonic": "R",
     "args": {
        "type": "subprocess",
        "encoding": "utf8",
        "cmd": ["python", "-i", "-u", "$file_basename"],
        "cwd": "$file_path",
        "syntax": "Packages/Python/Python.tmLanguage",
        "view_id": "*REPL* [python]",
        "external_id": "python",
        "extend_env": {"PYTHONIOENCODING": "utf-8"}
        }
    },

4) Using :

4.1) Open the Python file that you want to run in Sublime Text.

4.2) In Top Bar > "Tools" > "Build System" > "PythonRepl".

4.3) Build the Python file, by choosing In Top Bar > "Tools" > "Build"
or
Using either the build shortcut (Ctrl+B for Windows, or ⌘ Command+B for Mac)

Sujay U N
  • 4,974
  • 11
  • 52
  • 88
19

I want to expand on @sblair's response. @alexpmil asked in a comment how to prevent the REPL from closing.

  1. In Sublime, go to Sublime Text > Preferences > Browse Packages
  2. In your packages, open SublimeREPL\config\Python\Main.sublime-menu.
  3. Find the part that contains id: repl_python_run.
  4. Under args/cmd, add -i. That's it.

For reference, mine looks like the following:

{"command": "repl_open",
 "caption": "Python - RUN current file",
 "id": "repl_python_run",
 "mnemonic": "d",
 "args": {
     "type": "subprocess",
     "encoding": "utf8",
     "cmd": ["C:/Python34/python", "-u", "-i", "$file_basename"],
     "cwd": "$file_path",
     "syntax": "Packages/Python/Python.tmLanguage",
     "external_id": "python",
     "extend_env": {"PYTHONIOENCODING": "utf-8"}
 }
}
SalamEkshi
  • 33
  • 4
mohamedmoussa
  • 547
  • 4
  • 13
  • 1
    Do you know a way to re-use an already openned REPL tab ? The `-i` argument do prevents the REPL from closing, but when I re-build the same script, it will run in a new REPL tab. – Seeven Mar 07 '15 at 00:51
  • I have the same issue as Seeven Byakko – BCR Jul 19 '15 at 18:47
  • @Seeven To resue the same tab, add the following line in the "repl_python_run" command in SublimeREPL\config\Python\Main.sublime-menu, right before the "external_id": "python" argument: view_id": "*REPL* [python]" – Victor Di May 24 '21 at 18:06