How to Run Python Code in Sublime Text 3

Hiten Kanwar Feb 02, 2024
How to Run Python Code in Sublime Text 3

Sublime Text is a popular code editor. It supports many markup and programming languages, within which we can add functions via plugins, which are community-built and maintained under free software licenses. You can find the steps to install sublime on Ubuntu here if sublime is not installed yet.

This tutorial will discuss how to run Python code in sublime text 3.

We can run Python code inside the Sublime Text with the use of these built-in build systems. By pressing Ctrl+B, Sublime 3 will run the python code inside the integrated console (provided we have saved the file with .py file extension).

But a problem with this implementation is that with this method, we cannot perform interactive activities like performing user input functions using input() or any other interaction with the program.

For this, we press Ctrl+Shift+P in Windows and type Install Package Control. macOS users can use the Cmd+Shift+P for the same.

Then we need to install Terminus via Package Control. So we again press the same keys and type Package Control: Install Package and then type Terminus.

We then go to the Tools menu and select the Build System option. Here we choose New Build System and paste the code below.

For windows, change the path to 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, change the path to 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}\"",
        }
    ]
}

We can name the file and select this Custom Build System to run the code.

Related Article - Python Run