简体   繁体   中英

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. 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

Setup Sublime for python

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

  • Sublime Text 3 will run your python code inside the integrated console when you use Ctrl + B

  • 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

@Thayakorn Rakwetpakorn's answer is correct

Ctrl+ B, and also make sure to have saved the file as hello.py or something before trying to run it

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

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

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

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

  1. Install Terminus via Package Control:

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

  1. Create Build System for 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:

{
    "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.:

{
    "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
  2. Select the newly created Build System:

Tools ▶ Build System ▶ Python Custom 6. Execute your code: Ctrl/CMD + B

SOURCE

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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