Of course! Here is a comprehensive guide to using Sublime Text for Python development, covering everything from setup to advanced tips.

Why Use Sublime Text for Python?
Sublime Text is a lightweight, fast, and highly customizable code editor. While it's not a full-featured IDE like PyCharm or VS Code, its speed and simplicity make it a favorite among many Python developers. It's excellent for:
- Quick scripts and edits
- Light to medium-sized projects
- Working on slower machines
- Customizing your editor to perfection
Part 1: Installation and Setup
Install Sublime Text
- Go to the official Sublime Text website.
- Download and install the version for your operating system (Windows, macOS, or Linux).
- The free version is a fully functional trial, but you'll occasionally see a nag screen asking you to buy a license.
Install the Package Control Package Manager
This is the most important step. Package Control is a package manager that makes it incredibly easy to install and manage other packages.
-
Open Sublime Text.
-
Open the Console:
(图片来源网络,侵删)- Windows/Linux:
View > Show Console - macOS:
View > Show Console
- Windows/Linux:
-
Copy the code for your version of Sublime Text from the official Package Control installation page. As of recent versions, the code is usually:
import urllib.request, urllib.parse, urllib.error, os pkg_url = 'https://packagecontrol.io/install_st3.py' try: import ssl except ImportError: ssl = None if ssl: context = ssl.create_default_context() context.check_hostname = False context.verify_mode = ssl.CERT_NONE f = urllib.request.urlopen(pkg_url, context=context) else: f = urllib.request.urlopen(pkg_url) data = f.read() result = open(os.path.join(os.path.expanduser('~'), 'install_package_control.py'), 'wb') result.write(data) result.close() print('Package Control successfully installed.') -
Paste this code into the Console and press Enter.
-
Restart Sublime Text.
Install Essential Python Packages
Now that Package Control is installed, you can easily add the tools you need.

- Open the Command Palette:
- Windows/Linux:
Ctrl+Shift+P - macOS:
Cmd+Shift+P
- Windows/Linux:
- Type
Install Packageand press Enter. - Now, type the names of the packages you want to install and press Enter for each one. Here are the essentials:
| Package Name | What it Does |
|---|---|
| Anaconda | The ultimate Python package for Sublime. It provides autocompletion, code linting (pylint, flake8), code formatting (autopep8), and Goto Definition (F12). This is the one you should install first. |
| SublimeREPL | Allows you to run Python code (and other languages) directly inside Sublime Text. Great for interactive testing without leaving the editor. |
| GitGutter | Shows inline markers in the gutter (left margin) for added, modified, and deleted lines. Essential for version control. |
| SideBarEnhancements | Enhances the default file sidebar with many useful features like creating new files/folders, copying paths, and running commands. |
Part 2: Building Your Workflow
Running Python Code
There are a few ways to run your Python scripts.
Method 1: The Quick Way (for simple scripts)
- Save your file with a
.pyextension (e.g.,hello.py). - Press
Ctrl+B(orCmd+Bon macOS). This will run the script in the terminal, and the output will appear in a new output panel at the bottom of the screen.
Method 2: Using SublimeREPL (for interactive sessions)
- Install SublimeREPL as described above.
- Open your Python file.
- Open the Command Palette (
Ctrl+Shift+P). - Type
SublimeREPL: Python - RUN current fileand press Enter. - A new interactive Python REPL will open in a new tab, allowing you to interact with your code.
Code Completion and Linting with Anaconda
Anaconda is your best friend for Python-specific features.
- Autocompletion: As you type, a dropdown will suggest function and variable names. Press
Tabto accept a suggestion. - Code Linting: Anaconda will automatically check your code for errors and style issues (like PEP 8 violations). It will underline problematic code in red (errors) or yellow (warnings). Hover over the underline to see the message.
- Code Formatting: You can automatically format your code to conform to the PEP 8 style guide.
- Open the Command Palette (
Ctrl+Shift+P). - Type
Anaconda: Format Codeand press Enter.
- Open the Command Palette (
- Goto Definition: Place your cursor on a function or variable name and press
F12to jump to its definition. PressCtrl+-(orCmd+-) to jump back.
Building a Project
- Go to
File > Open Folder...and select the root folder of your Python project. - Sublime Text will create a "workspace" for you, and you'll see your project files in the sidebar.
- You can create new files and folders directly from the enhanced sidebar (
SideBarEnhancementsis recommended for this).
Part 3: Customization and Advanced Tips
User Preferences
This is where you can change Sublime Text's behavior to your liking.
- Open the Command Palette (
Ctrl+Shift+P). - Type
Preferences: Settingsand press Enter. This opens the default settings. - To add your own custom settings, click
Preferences: Settings - User. This file will override the default settings.
Example Preferences.sublime-settings (User) file:
{
// Set the default encoding to UTF-8
"default_encoding": "UTF-8",
// Show line numbers
"line_numbers": true,
// Highlight the current line
"highlight_line": true,
// Set the default indentation to 4 spaces
"tab_size": 4,
"translate_tabs_to_spaces": true,
// Don't show the welcome message on startup
"show_startup_console": false,
// Anaconda-specific settings
"anaconda_linter": {
"pep8": {
"ignore": ["E501"] // Ignore line length errors
}
}
}
Custom Key Bindings
You can create your own keyboard shortcuts.
- Open the Command Palette (
Ctrl+Shift+P). - Type
Key Bindings - Userand press Enter. This opens a JSON file where you can define your shortcuts.
Example Default.sublime-keymap (User) file:
[
// Example: Run Python with Ctrl+Shift+B instead of Ctrl+B
{ "keys": ["ctrl+shift+b"], "command": "exec", "args": {"kill": true, "cmd": ["python", "-u", "$file"]}},
// Example: Format code with Ctrl+Shift+F
{ "keys": ["ctrl+shift+f"], "command": "anaconda_format_code" }
]
Snippets
Snippets are small, reusable pieces of code. You can create your own.
- Navigate to
Tools > Developer > New Snippet.... - A template file will be created. Edit it to create your snippet.
Example Python function snippet:
<snippet>
<content>
def ${1:function_name}(${2:arguments}):
"""
${3:Docstring for the function.}
"""
${4:# Your code here}
pass
</content>
<!-- Tab trigger to activate the snippet -->
<tabTrigger>def</tabTrigger>
<!-- Scope is where the snippet is active. 'source.python' means it only works in Python files. -->
<scope>source.python</scope>
</snippet>
Save this file as function.sublime-snippet in your Packages/User folder. Now, when you type def and press Tab, the snippet will appear.
Part 4: The Sublime Text Ecosystem
- Themes: You can completely change the look and feel. Search for "Theme" in Package Control to find popular ones like
Material ThemeorSoda Dark. - Color Schemes: These change the syntax highlighting. Search for "Color Scheme" in Package Control.
- Plugins: There are thousands of other plugins for everything from Git integration to Markdown editing. The Sublime Text forums and Package Control are great places to find them
