当前位置: 首页>>技术问答>>正文


如何在SublimeREPL上运行Python代码

, , ,

问题描述

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

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

先感谢您!

最佳解决方法

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

可选(要检查上面的软件包是否已成功安装:单击Preferences > Browse Packages …在此文件夹中单击Back Button一次,然后进入Installed Packages /文件夹,检查是否有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

你完成了

次佳解决方法

作为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-python。现在,Ctrl + B应该执行当前的Python文件,并在新选项卡中输出。如果使用两列布局,则REPL输出应在第二列中打开。 (这是使用Sublime Text 3.)

第三种解决方法

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

  1. 在您的包中,打开SublimeREPL\config\Python\Main.sublime-menu

  2. 找到包含id的零件:repl_python_run

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

第四种方法

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

1)安装SublimeREPL插件:

In Top Bar > “Tools” > “Command Palette” > “Package Control: Install Package”
Search for : “SublimeREPL” and install


2)创建构建系统:

In Top Bar > “Tools” > “Build System” > “New Build System”

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

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

将文件保存为默认”user”文件夹中的”PythonRepl.sublime-build”。


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

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

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)

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/7059.html,未经允许,请勿转载。