38

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!

4

4 回答 4

76

如此处所述,创建一个新的构建系统文件并将其另存为..\Packages\User\SublimeREPL-python.sublime-build. 该文件应包含:

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

然后转到您的 Python 文件选项卡并选择工具 > 构建系统 > SublimeREPL-python。现在,Ctrl+B应该执行当前的 Python 文件,并在新选项卡中输出。如果您使用双列布局,则 REPL 输出应在第二列中打开。(这是使用 Sublime Text 3。)

于 2014-05-18T13:50:03.083 回答
30

第一个“ Install Package Control”来自https://sublime.wbond.net/installation#st2

可选查看上面的包是否安装成功:Preferences > Browse Packages在这个文件夹中点击...点击Back Button一次,然后进入Installed Packages/文件夹,查看是否有Package Control.sublime-package文件

然后去Preferences > Package Control > Package Control: Install Package 崇高的文字2

SublimeREPL在列表中查找。

重启 SublimeText2

打开Preferences > Package Settings > SublimeREPL > Settings - Default文件从那里复制所有文本。

然后在此处打开Preferences > Package Settings > SublimeREPL > Settings - User并粘贴文本。

重启 SublimeText2

Tools > SublimeREPL > Python > Python

你完成了

于 2013-11-01T18:00:34.870 回答
24

制作交互式和可重用的 Sublime Python 控制台的步骤:

1) 安装 SublimeREPL 插件:

在顶栏>“工具”>“命令面板”>“包控制:安装包”
搜索:“SublimeREPL”并安装


2)创建构建系统:

在顶部栏中 >“工具”>“构建系统”>“新构建系统”

将文件的所有内容替换为:

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

在默认的“用户”文件夹中将文件另存为“PythonRepl.sublime-build”。


3) 使控制台交互和可重用的设置:

|=> 转到“首选项”>“浏览包”

|=> 转到文件夹:SublimeRepl

|=> 编辑:sublimerepl.py

Replace : if view.id() == view_id

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

|=> 转到文件夹:SublimeRepl/config/Python

|=> 编辑: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)使用:

4.1) 在 Sublime Text 中打开要运行的 Python 文件。

4.2)在顶部栏>“工具”>“构建系统”>“PythonRepl”。

4.3) 构建 Python 文件,方法是选择 In Top Bar > "Tools" > "Build"

使用构建快捷方式(Windows 为 Ctrl+B,Mac 为 ⌘ Command+B)

于 2018-01-23T20:26:34.633 回答
18

我想扩展@sblair 的回复。@alexpmil 在评论中询问如何防止 REPL 关闭。

  1. 在 Sublime 中,转到 Sublime Text > Preferences > Browse Packages
  2. 在您的包中,打开SublimeREPL\config\Python\Main.sublime-menu.
  3. 找到包含id:的部分repl_python_run
  4. 在 下args/cmd,添加-i。而已。

作为参考,我的如下所示:

{"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"}
 }
}
于 2014-10-27T18:21:00.230 回答