Python
Sublime Text 2
Code Execution
Programming
Development Environment

How do I run Python code from Sublime Text 2?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Sublime Text 2 can run Python files through its build system, even though it is just an editor and not a full IDE. The key is understanding that Sublime launches an external Python interpreter, so most setup problems come from choosing the wrong interpreter path or running from the wrong working directory.

Use the Default Python Build First

The simplest case is a saved .py file and a working Python installation. Open the file, then choose Tools -> Build System -> Python. After that, run Tools -> Build or press Ctrl+B.

For a file like this:

python
print("Hello from Sublime Text 2")

Sublime sends the file to the configured Python executable and shows output in the build panel.

Before blaming Sublime, confirm Python works outside the editor:

bash
python --version

On some systems the command is python3 instead:

bash
python3 --version

If the terminal cannot find Python, Sublime will not find it either.

Understand What the Build System Does

A Sublime build system is just a small JSON file that describes how to run a command. The stock Python build usually looks conceptually like this:

json
1{
2  "cmd": ["python", "-u", "$file"],
3  "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
4  "selector": "source.python"
5}

The important part is cmd. That command runs your current file using python -u, where -u turns off output buffering so printed text appears immediately.

If your machine uses python3, or if you want a virtual environment interpreter, create a custom build system.

Create a Custom Build System

Go to Tools -> Build System -> New Build System... and save a file such as Python3.sublime-build.

Example for a machine where the correct command is python3:

json
1{
2  "cmd": ["python3", "-u", "$file"],
3  "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
4  "selector": "source.python"
5}

After saving it, select that build system from the menu and run Ctrl+B again.

If you are on Windows and want to point directly at a specific interpreter, use its absolute path:

json
1{
2  "cmd": ["C:/Python311/python.exe", "-u", "$file"],
3  "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
4  "selector": "source.python"
5}

This is often more reliable than relying on PATH.

Set the Working Directory Correctly

A script that reads local files may fail even though it runs fine from a terminal. That usually means the working directory is different.

You can fix that in the build file:

json
1{
2  "cmd": ["python3", "-u", "$file"],
3  "working_dir": "$file_path",
4  "selector": "source.python"
5}

Now relative file paths are resolved from the folder containing the script.

Test it with:

python
from pathlib import Path

print("Current working directory:", Path.cwd())

If the printed directory is not what you expect, adjust working_dir.

Use a Virtual Environment When Projects Depend on Packages

If your code imports packages that are not installed globally, the build system must use the interpreter inside your virtual environment.

For example:

json
1{
2  "cmd": ["/Users/mark/project/.venv/bin/python", "-u", "$file"],
3  "working_dir": "$file_path",
4  "selector": "source.python"
5}

On Windows, the path usually ends in Scripts/python.exe.

This is the cleanest way to make Sublime run the same interpreter as your terminal project environment.

Interactive Input Is Limited

The normal Sublime build panel is not a full interactive terminal. Scripts that call input() often hang or fail because the panel does not provide standard input the way a terminal does.

This simple script works in a terminal:

python
name = input("Name: ")
print("Hello", name)

In Sublime Text 2, you may need a plugin such as Terminus in newer Sublime versions, or run the script in an external terminal instead. For non-interactive scripts, the build system is fine. For interactive programs, a terminal is usually the right tool.

Debugging Common Build Failures

If the build output says python: command not found, the interpreter path is wrong.

If imports fail, you are probably using the wrong interpreter or a missing virtual environment.

If file reads fail, the working directory is wrong.

If nothing runs, make sure the file is saved first. Sublime builds the current file on disk, not an unsaved buffer state.

Common Pitfalls

Selecting the wrong build system. Confirm Tools -> Build System -> Python or your custom Python build is active.

Assuming python and python3 are interchangeable on every machine. They are not.

Using a global interpreter for a project that depends on a virtual environment.

Running scripts that require input() through the standard build panel.

Forgetting to save the file before building.

Summary

  • Sublime Text 2 runs Python through a build system that launches an external interpreter.
  • Start with the default Python build, then create a custom one if you need python3 or a virtual environment.
  • Set working_dir when your script uses relative paths.
  • Use a project-specific interpreter for package-managed code.
  • For interactive programs, run the script in a real terminal instead of the build panel.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.