如何在SublimeREPL上运行Python代码

时间:2013-11-01 17:28:01

标签: python ubuntu python-2.7 sublimerepl

我真的很喜欢使用sublime text 2来编写Python代码,但是每当我尝试运行具有输入的脚本时,sublime文本控制台都会报告错误。所以,我决定尝试SublimeREPL,但是我一直在搜索几个小时而且我没有找到如何运行Python代码...... 你能帮助我吗?

我想在SublimeREPL上运行代码,就像我们使用sublime文本控制台(CTRL+b)一样。我真正想知道的是,是否有办法做同样的事情SublimeREPL

提前谢谢!

4 个答案:

答案 0 :(得分:70)

作为described here,创建一个新的Build System文件并将其另存为..\Packages\User\SublimeREPL-python.sublime-build。该文件应包含:

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

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

答案 1 :(得分:25)

来自https://sublime.wbond.net/installation#st2

的第一个“Install Package Control

可选要检查上面的软件包是否已成功安装: 单击此文件夹中的Preferences > Browse Packages .... 单击Back Button一次,然后进入已安装的软件包/文件夹,检查是否有Package Control.sublime-package文件

然后转到崇高文字2中的Preferences > Package Control > Package Control: Install Package

在列表中找到SublimeREPL

重新启动SublimeText2

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

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

重新启动SublimeText2

转到Tools > SublimeREPL > Python > Python

你已经完成了

答案 2 :(得分:18)

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

  1. 在您的包裹中,打开SublimeREPL\config\Python\Main.sublime-menu
  2. 找到包含id的部分:repl_python_run
  3. args/cmd下,添加-i。那就是它。
  4. 作为参考,我的内容如下:

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

答案 3 :(得分:9)

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

1)安装SublimeREPL插件:

  

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

2)创建构建系统:

  

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

用以下内容替换文件的所有内容:

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

将文件保存为默认“user”文件夹中的“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)通过选择In Top Bar>构建Python文件。 “工具”> “建设”
      或
      使用构建快捷方式(Windows为Ctrl + B,Mac为⌘Command+ B)