How to Run Python Code on SublimeREPL

一曲冷凌霜 提交于 2019-11-26 17:56:28

问题


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



回答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)



来源:https://stackoverflow.com/questions/19732006/how-to-run-python-code-on-sublimerepl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!