How to Run Python Code on SublimeREPL
Asked Answered
P

4

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!

Pierrepierrepont answered 1/11, 2013 at 17:28 Comment(2)
Did you read the documentation? What exactly are you having a problem with? Please 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.Goeger
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 yetPierrepierrepont
E
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

Expert answered 1/11, 2013 at 18:0 Comment(6)
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)..Pierrepierrepont
Yes, simply write and save your code on Sublime text2 editor. And after save go to Tools > SublimeREPL > Python > Python-RUN Current fileExpert
thanks for your answer! it was very helpfull! btw, there's a shortcut for that command?Pierrepierrepont
@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.Goeger
Is this the same when I'm coding in Ruby or something like that ?Treacherous
Whats the solution for Sublime Text 3 ? does sublimeREPL works for ST3 too ? or if not whats the solution.Fidellas
A
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.)

Alidaalidade answered 18/5, 2014 at 13:50 Comment(6)
This is absolutely perfect and worked first time. I don't understand why SublimeREPL doesn't do this out of the box. Thank you.Teofilateosinte
@Alidaalidade 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?Romonaromonda
@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!Alidaalidade
@sblair, thanks, I use this to prevent the REPL from closing when sys.exit() is called.Griffith
This is the answer that should be accepted, works perfectly. Thank you!Montymonument
@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: youtube.com/watch?v=wM2LbXCkLDISyllabi
E
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

Expert answered 1/11, 2013 at 18:0 Comment(6)
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)..Pierrepierrepont
Yes, simply write and save your code on Sublime text2 editor. And after save go to Tools > SublimeREPL > Python > Python-RUN Current fileExpert
thanks for your answer! it was very helpfull! btw, there's a shortcut for that command?Pierrepierrepont
@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.Goeger
Is this the same when I'm coding in Ruby or something like that ?Treacherous
Whats the solution for Sublime Text 3 ? does sublimeREPL works for ST3 too ? or if not whats the solution.Fidellas
P
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)

Pastry answered 23/1, 2018 at 20:26 Comment(7)
This is exactly what I wanted. Tried many other answers but none didn't work. Thanks for this, works like a charmNecessitous
you can also move interactive tab with the output to the second column and stop switching back and forth between two tabs :).Dibucaine
Perfect answer, Worked like a charm for me. +10Honeycomb
@VictorDi how to move the output to the 2nd column. can you please add your answer?Honeycomb
@Honeycomb View > Layout > Columns:2, then drag your Repl console to the second column.Dibucaine
Very nice - it really helps to have a step-by-step guide, as the Sublime Text package config can feel a bit intimidatingSouthwestwardly
Awesome! - nice jobTombac
P
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"}
 }
}
Palindrome answered 27/10, 2014 at 18:21 Comment(3)
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.Improper
I have the same issue as Seeven ByakkoMontage
@Improper 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]"Dibucaine

© 2022 - 2024 — McMap. All rights reserved.