如何在 SublimeREPL 上运行 Python 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19732006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:27:42  来源:igfitidea点击:

How to Run Python Code on SublimeREPL

pythonubuntupython-2.7sublimerepl

提问by wombatp

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?

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

I want to run the code on SublimeREPLas 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.

我想像SublimeREPL使用 sublime 文本控制台 ( CTRL+b)一样运行代码。我真正想知道的是,是否有一种方法可以对SublimeREPL.

Thank you in advance!

先感谢您!

采纳答案by Nishant Bhakta

First "Install Package Control" from https://sublime.wbond.net/installation#st2

第一个“ Install Package Control”来自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 Buttonone time and then into the Installed Packages/ folder, check there will be Package Control.sublime-packagefile)

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

then go to Preferences > Package Control > Package Control: Install Packagein sublime text 2

然后转到Preferences > Package Control > Package Control: Install Packagesublime text 2

find SublimeREPLin list.

SublimeREPL在列表中查找。

Restart SublimeText2

重启 SublimeText2

open Preferences > Package Settings > SublimeREPL > Settings - Defaultfile copy all text from there.

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

then open Preferences > Package Settings > SublimeREPL > Settings - Userand paste the text here.

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

Restart SublimeText2

重启 SublimeText2

Go to Tools > SublimeREPL > Python > Python

Tools > SublimeREPL > Python > Python

And you are done

你已经完成了

回答by sblair

As described here, create a new Build System file and save it as ..\Packages\User\SublimeREPL-python.sublime-build. The file should contain:

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

{
    "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+ Bshould 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.)

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

回答by mohamedmoussa

I want to expand on @sblair's response. @alexpmil asked in a comment how to prevent the REPL from closing.

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

  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.
  1. 在 Sublime 中,转到 Sublime Text > Preferences > Browse Packages
  2. 在您的包中,打开SublimeREPL\config\Python\Main.sublime-menu.
  3. 找到包含id:的部分repl_python_run
  4. 在 下args/cmd,添加-i. 就是这样。

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

回答by Sujay U N

Steps to make Sublime Python Console which is Interactive and Reusable :

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

1) Install SublimeREPL plugin :

1) 安装 SublimeREPL 插件:

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

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



2) Create Build System :

2)创建构建系统:

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.

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



3) Settings to make Console interactive and Reusable:

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

|=> Goto "Preferences" > "Browse Packages"

|=> Goto Folder : SublimeRepl

|=> Edit : sublimerepl.py

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

|=> 转到文件夹:SublimeRepl

|=> 编辑:sublimerepl.py

Replace : if view.id() == view_id

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


|=> Goto Folder : SublimeRepl/config/Python

|=> Edit : Main.sublime-menu

|=> 转到文件夹: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) Using :

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)

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

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

4.3) 构建 Python 文件,通过选择 In Top Bar > "Tools"> "Build"

使用构建快捷方式(Ctrl+B for Windows,或 ? Command+B for Mac)