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.
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:
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:
On some systems the command is python3 instead:
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:
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:
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:
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:
Now relative file paths are resolved from the folder containing the script.
Test it with:
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:
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:
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
python3or a virtual environment. - Set
working_dirwhen 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
- How do I sample a line across a blob at a perpendicular angle? in Python/OpenCV unless you suggest switching to something else
- How do I select certain columns of a 2D tensor in TensorFlow?
- How do I select rows from a DataFrame based on column values?
- How do I send something to connected websocket clients from another thread?
- How do I set the maximum line length in PyCharm?
- How do I solve error externally-managed-environment every time I use pip 3?
- How do I solve overfitting in random forest of Python sklearn?
- How do I sort a dictionary by key?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.