How to Run Python Code on SublimeREPL

前端 未结 4 627
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 17:09

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

相关标签:
4条回答
  • 2020-11-29 17:31

    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)

    0 讨论(0)
  • 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"}
     }
    }
    
    0 讨论(0)
  • 2020-11-29 17:41

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

    0 讨论(0)
  • 2020-11-29 17:42

    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

    0 讨论(0)
提交回复
热议问题