简体   繁体   English

如何在 Sublime Text 3 中运行 python 代码?

[英]How to run python code in Sublime Text 3?

So I'm trying to run python code from Sublime Text 3, but I'm not sure how.所以我试图从 Sublime Text 3 运行 python 代码,但我不确定如何。 Even if it was only from the console, that would be fine.即使它只是来自控制台,那也没关系。 Anybody know how???有谁知道怎么办???

工具->构建系统->Python 或 Ctrl+B

需要安装包才能从 sublime Python + Sublime运行 python

Try Anaconda plugin which will help you to run python on sublime试试Anaconda插件,它可以帮助你在 sublime 上运行 python

Setup Sublime for python 为 python 设置 Sublime

您可以在 sublime text 中使用此包: https : //packagecontrol.io/packages/Terminal在特定文件或文件夹中打开终端。

  • Sublime Text 3 will run your python code inside the integrated console when you use Ctrl + B当您使用 Ctrl + B 时,Sublime Text 3 将在集成控制台中运行您的 Python 代码

  • if you want to run your code at own terminal, but still get some error information inside to integrated console, you need to build your own builder, or use plugins.如果您想在自己的终端上运行代码,但仍然在集成控制台中获取一些错误信息,则需要构建自己的构建器,或使用插件。

  • One way: https://github.com/Wilhox/Integrated-builder.git一种方式: https : //github.com/Wilhox/Integrated-builder.git

@Thayakorn Rakwetpakorn's answer is correct @Thayakorn Rakwetpakorn 的回答是正确的

Ctrl+ B, and also make sure to have saved the file as hello.py or something before trying to run it Ctrl+ B,并确保在尝试运行之前将文件保存为 hello.py 或其他内容

If that doesnt work Tools->Build system -> New build system如果这不起作用工具->构建系统->新构建系统

Comment the already existing code as i have shown below评论已经存在的代码,如下所示

{
    //"shell_cmd": "make"
    "cmd": ["C:\\Users\\Programs\\Python\\Python37\\python.exe","-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector': "source.python"
}

Type this set of code, and change the file location to where your python.exe file location is, in above code lines键入这组代码,并将文件位置更改为您的 python.exe 文件位置,在上面的代码行中

"C:\\Users\\Programs\\Python\\Python37\\python.exe"

Instead of the code lines of my file location path而不是我的文件位置路径的代码行

This answer is for fellow googlers who want to run python scripts within their sublime.这个答案适用于想要在他们的 sublime 中运行 python 脚本的谷歌员工。 As other answers explains, all you need is a sublime build system, here after a bit of struggle I got it worked for Linux Systems.正如其他答案所解释的那样,您只需要一个崇高的构建系统,经过一番努力,我让它适用于 Linux 系统。

{
  "cmd": ["gnome-terminal", "--", "/bin/bash", "-c", "python3 -u \"$file\" echo;echo;echo Press Enter to exit...;read"],
  "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
  "selector": "source.python"
}

This is by far the simplest I believe.这是迄今为止我认为最简单的。 Hope it helps.希望它有帮助。

If you need non-interactive Build System, just follow the Official Guide .如果您需要非交互式构建系统,只需遵循官方指南

If you plan to run code that contains something like input() , or you have any other way of interacting with your program you would need additional setup - Plugin + simple config.如果您打算运行包含input() 之类的代码,或者您有任何其他方式与您的程序交互,则需要额外的设置 - 插件 + 简单配置。

The steps of having proper/full build system are:拥有适当/完整构建系统的步骤是:

  1. Install "Package Control":安装“包控制”:

Win/Linux: ctrl+shift+p , Mac: cmd+shift+p ▶ Type: Install Package Control ▶ ENTER Win/Linux: ctrl+shift+p , Mac: cmd+shift+p ▶ Type: Install Package Control ▶ ENTER

  1. Install Terminus via Package Control:通过包控制安装Terminus

Win/Linux: ctrl+shift+p , Mac: cmd+shift+p ▶ Type: Package Control: Install Package ▶ ENTER ▶ Type: Terminus ▶ ENTER Win/Linux: ctrl+shift+p , Mac: cmd+shift+p ▶ Type: Package Control: Install Package ▶ ENTER ▶ Type: Terminus ▶ ENTER

  1. Create Build System for Python为 Python 创建构建系统

Tools ▶ Build System ▶ New Build System… menu item or the Build: New Build System ▶ Paste one of the following sections and edit respectively.工具 ▶ 构建系统 ▶ 新建构建系统... 菜单项或构建:新建构建系统 ▶ 粘贴以下部分之一并分别进行编辑。

For Windows , obviously you should change the path to your Python:对于Windows ,显然您应该更改 Python 的路径:

{
    "target": "terminus_exec",
    "cancel": "terminus_cancel_build",
    
    "shell_cmd": "D:\\.python_venvs\\general_python\\Scripts\\python.exe -u \"$file\"",
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",

    "env": {"PYTHONIOENCODING": "utf-8"},

    "variants":
    [
        {
            "name": "Syntax Check",
            "shell_cmd": "D:\\.python_venvs\\general_python\\Scripts\\python.exe -m py_compile \"${file}\"",
        }
    ]
}

For Mac/Linux , do not forget to change the path to your Python.:对于Mac/Linux ,不要忘记更改 Python 的路径。:

{
    "target": "terminus_exec",
    "cancel": "terminus_cancel_build",
    
    "shell_cmd": "/home/<user>/.python_venvs/general_python/Scripts/python -u \"$file\"",
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",

    "env": {"PYTHONIOENCODING": "utf-8"},

    "variants":
    [
        {
            "name": "Syntax Check",
            "shell_cmd": "/home/<user>/.python_venvs/general_python/Scripts/python -m py_compile \"${file}\"",
        }
    ]
}
  1. Name the file eg: Python Custom.sublime-build命名文件,例如:Python Custom.sublime-build
  2. Select the newly created Build System:选择新创建的构建系统:

Tools ▶ Build System ▶ Python Custom 6. Execute your code: Ctrl/CMD + B工具 ▶ 构建系统 ▶ Python 自定义 6. 执行您的代码:Ctrl/CMD + B

SOURCE来源

键入 ctrl+B 或 ctrl+shift+p 此外访问此网站https://www.delftstack.com/howto/python/python-run-code-in-sublime-text/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM