Of course! Using Python with Sublime Text 3 is a very common and powerful workflow. Here’s a comprehensive guide covering everything from basic setup to running code and advanced features.

Installation and Prerequisites
A. Install Sublime Text 3
If you haven't already, download and install Sublime Text 3 from the official website: https://www.sublimetext.com/3
B. Install Python
You need Python installed on your system.
- Windows: Download from python.org. Crucially, check the box "Add Python to PATH" during installation.
- macOS: Usually comes pre-installed. You can verify by opening Terminal and typing
python3 --version. If not, use Homebrew:brew install python. - Linux: Usually comes pre-installed. Verify with
python3 --versionin the terminal.
C. Verify Python Installation
Open your system's terminal or command prompt and run:
# For Python 3 python3 --version # Or on some systems, just 'python' might point to Python 3 python --version
This should show the installed version (e.g., Python 3.11.4).

The Basics: Writing and Running Python Code
A. Create a Python File
- Open Sublime Text 3.
- Go to
File > New File. - Save the file with a
.pyextension (e.g.,hello.py).
B. Write Your First Script
In the new file, type the classic "Hello, World!" script:
# hello.py
print("Hello, World!")
C. Run the Script
There are two primary ways to run your Python code from Sublime Text.
Method 1: Using the Terminal (Recommended)
This is the most flexible and common method.
- Save your Python file (
hello.py). - Open the Sublime Text built-in terminal:
- Go to
Tools > Build System > Python(this sets the correct interpreter). - Then, go to
Tools > Build System > New Build System.... A new file will open. Replace its contents with this:{ "cmd": ["python3", "-u", "$file"], "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)", "selector": "source.python" } - Save this file with a memorable name, like
Python3.sublime-build, in the default location Sublime suggests. This tells Sublime to usepython3to run the file.
- Go to
- Now, with your
hello.pyfile open, simply press:Ctrl + B(on Windows/Linux)Cmd + B(on macOS)
The output Hello, World! will appear in a new "output" panel at the bottom of the Sublime window.

Method 2: Using the SublimeREPL Plugin (For Interactive Sessions)
This plugin allows you to run an interactive Python shell inside Sublime Text, which is great for testing small snippets of code.
-
Install Package Control:
- If you don't have it, open Sublime and use the View menu to show the console (
View > Show Console). - Paste the appropriate code for your OS from the Package Control website into the console and press Enter. This is the single most important plugin for Sublime.
- If you don't have it, open Sublime and use the View menu to show the console (
-
Install SublimeREPL:
- Press
Ctrl + Shift + P(orCmd + Shift + Pon Mac) to open the Command Palette. - Type
Package Control: Install Packageand press Enter. - In the new list that appears, type
SublimeREPLand select it to install.
- Press
-
Run SublimeREPL:
- Open your Python file (
hello.py). - Open the Command Palette (
Ctrl+Shift+P). - Type
SublimeREPL: Python - RUN current fileand press Enter. - A new tab will open with an interactive Python shell, and your script will be executed in it.
- Open your Python file (
Essential Plugins for Python Development
Package Control makes installing these a breeze. Use Ctrl+Shift+P -> Package Control: Install Package for each one.
| Plugin | Purpose | Why You Need It |
|---|---|---|
| LSP (Language Server Protocol) | The modern standard for IDE features. Provides real-time error checking, autocompletion, go-to-definition, and more. It's the foundation for a great Python experience in Sublime. | Essential. Turns Sublime from a text editor into a lightweight IDE. It supports many languages, including Python. |
| Anaconda | A powerful all-in-one package for Python development. | A great alternative to LSP, especially if you want a simpler setup. It provides linting, autocompletion, and code navigation. |
| Black Formatter | Integrates the Black code formatter. | Automatically formats your Python code to be clean and consistent, adhering to the PEP 8 style guide. Run it with Ctrl+Shift+P -> Black: Format File. |
| Flake8 | Integrates the Flake8 linter. | Scans your code for style and syntax errors (violations of PEP 8). Warnings will appear in the editor, helping you catch mistakes early. |
| SublimeJEDI | Provides intelligent autocompletion. | Works well with Anaconda or on its own. It understands your code and suggests variables, functions, and modules as you type. |
| DocBlockr | Simplifies writing docstrings. | Automatically generates PEP 257 compliant docstrings for your functions and classes. Just type above a function and press Tab. |
Setting Up a Project with Virtual Environments
Using virtual environments is a best practice to manage project dependencies. Here's how to set it up with Sublime.
-
Create a Project Folder:
mkdir my_python_project cd my_python_project
-
Create a Virtual Environment:
# For Python 3, it's best to use 'venv' python3 -m venv venv
This creates a
venvfolder in your project. -
Activate the Environment:
- Windows:
venv\Scripts\activate - macOS/Linux:
source venv/bin/activateYou'll see(venv)at the beginning of your terminal prompt, indicating it's active.
- Windows:
-
Install Packages: Now, any
pip installwill go into this isolated environment.pip install requests pandas
-
Tell Sublime to Use the Virtual Environment's Python:
- Open your project in Sublime (
File > Open Folder...). - Open the Command Palette (
Ctrl+Shift+P). - Select
LSP: Select LSP. - Choose
Pylsp(if using LSP) or configure your build system to point to the Python interpreter inside thevenvfolder.- Windows:
venv\Scripts\python.exe - macOS/Linux:
venv/bin/python
- Windows:
- You can create a new build system (as in section 2.C) that uses this specific path.
- Open your project in Sublime (
Now, Sublime's linter, autocompletion, and runner will all use the packages from your venv.
Debugging Python Code
Sublime Text is not a full-fledged debugger like VS Code or PyCharm, but you can add basic debugging capabilities.
Option A: Using the pdb Module (Built-in)
Python has a built-in debugger called pdb. You can insert breakpoints directly into your code.
-
In your Python script, import
pdband add a breakpoint where you want to pause execution:# my_script.py import pdb def add(a, b): result = a + b pdb.set_trace() # Execution will stop here return result print(add(5, 3)) -
Run the script using the build system (
Ctrl+B). -
Execution will pause at
pdb.set_trace(). A new console will appear at the bottom where you can interact with the debugger using commands like:n(next): Execute the current line and move to the next.c(continue): Continue execution until the next breakpoint.l(list): Show the code around the current line.p <variable_name>(print): Print the value of a variable.
Option B: Using the Sublime Debugger Plugin
For a more graphical debugging experience, you can use a plugin like SublimeDebugger. This requires more setup but provides a more traditional debugger interface with breakpoints, variable inspection, etc. You would install it via Package Control.
Summary Workflow
- Setup: Install Sublime Text 3 and Python. Install Package Control.
- Install Core Plugins: Install LSP for a modern IDE experience.
- Create Project: Make a folder for your project and open it in Sublime.
- Virtual Environment: Create and activate a
venvinside your project folder. Install project dependencies. - Configure: Tell Sublime's LSP or build system to use the Python interpreter from your
venv. - Code: Write your Python code. Enjoy real-time linting and autocompletion from LSP.
- Run: Press
Ctrl+Bto quickly run your script and see the output. - Format/Lint: Use
Black: Format Fileto format your code and rely on the linter to catch errors. - Debug: Use
pdb.set_trace()for simple debugging when needed.
