How to Run Python Code on SublimeREPL

2019-01-12 19:18发布

问题:

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!

回答1:

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



回答2:

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



回答3:

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

  1. In your packages, open SublimeREPL\config\Python\Main.sublime-menu.
  2. Find the part that contains id: repl_python_run.
  3. 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"}
 }
}


回答4:

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)