WindowsMacSoftwareSettingsSecurityProductivityLinuxAndroidPerformanceConfigurationApple All

How to Set Up Sublime Text for Python Development

Edited 1 month ago by ExtremeHow Editorial Team

Sublime TextPythonDevelopmentWindowsMacLinuxProgrammingConfigurationIDEUser GuideTools

How to Set Up Sublime Text for Python Development

This content is available in 7 different language

Sublime Text is a popular text editor known for its simplicity, ease of use, and wide range of features. It's lightweight yet powerful, making it a great choice for both beginners and experienced developers working on Python projects. In this guide, we'll walk you through setting up Sublime Text for Python development. We'll cover everything you need from installation to configuration and include some tips and best practices for an optimal development environment.

Step 1: Installing Sublime Text

First, you need to install Sublime Text on your computer. You can download the latest version from the official Sublime Text website. Choose the appropriate version for your operating system, whether it's Windows, macOS or Linux.

  1. Visit the official Sublime Text website.
  2. Download the installer for your operating system (Windows, macOS, or Linux).
  3. Run the installer and follow the on-screen instructions.
  4. Once the installation is complete, open Sublime Text to proceed with the setup.

Step 2: Installing Package Control

Package Control is an important feature of Sublime Text that allows you to easily install plugins. It is essential for customizing your development environment. Here's how to install it:

  1. Open Sublime Text.
  2. Press Ctrl + ` (or View > Show Console from the menu) to open the Sublime Text console.
  3. Copy the installation script found on the Package Control installation page and paste it into the console and press Enter.
  4. Once the command completes, restart Sublime Text.

Now, you have Package Control installed, which will help you add various packages and plugins required for Python development.

Step 3: Installing Python plugins

To improve Sublime Text for Python development, we need to install several plugins that provide syntax highlighting, linting, autocompletion, and other useful features. Here are some recommended plugins:

Installing linters: SublimeLinter and SublimeLinter-pyflakes

Linters help check for errors in your code. SublimeLinter, along with a specific linter for Python such as SublimeLinter-pyflakes, can be very useful.

  1. Press Ctrl + Shift + P (or Cmd + Shift + P on macOS) to open the Command Palette.
  2. Package Control: Install Package and select it.
  3. Search for SublimeLinter and install it.
  4. After installation, repeat the process and install SublimeLinter-pyflakes.

Installing autocompletion: Anaconda

To get Intellisense-like features, you can use the Anaconda plugin for code completion, linting, and other tasks:

  1. Open the Command Palette with Ctrl + Shift + P (or Cmd + Shift + P on macOS).
  2. Package Control: Install Package.
  3. Search for Anaconda and install it.

Setting up the build system

You can set up a custom build system to run Python scripts directly from Sublime Text:

  1. Go to Tools > Build System > New Build System...
  2. A new file will open. Replace its contents with the following:
 
{ "cmd": ["python", "-u", "$file"], "file_regex": "^[ ]*File \\"(...?)\\", line ([0-9]*)", "selector": "source.python" }
  1. Save this file as Python.sublime-build.
  2. Now go to Tools > Build System and select Python.

This setup allows you to run Python scripts using the keyboard shortcut Ctrl + B on Windows/Linux or Cmd + B on macOS.

Step 4: Configuring Sublime Text preferences

Customizing your Sublime Text preferences can improve your productivity. Here are some settings you may find useful:

Indentation settings

Python relies on indentation, so it's important to get it right.

 
{ "translate_tabs_to_spaces": true, "tab_size": 4 }

You can add these settings to your Preferences.sublime-settings file located in Preferences > Settings.

Syntax specific settings

Set Python-specific preferences by going to Preferences > Settings > Syntax Specific > Python. Add the following to the Python-specific settings file:

 
{ "python_interpreter": "/usr/bin/python3" }

If /usr/bin/python3 is different, make sure to replace it with the path to your Python interpreter. You can find this path by executing which python3 in your terminal (Linux/macOS) or where python on Windows.

Key bindings

Custom key bindings can help speed up your development process. You define these in the Key Bindings file located in Preferences > Key Bindings. Here's an example of a custom key binding for quickly commenting and uncommenting lines of code:

 
[ { "keys": ["ctrl+/"], "command": "toggle_comment", "args": { "block": false } } ]

Step 5: Using the virtual environment

Virtual environments allow you to manage dependencies for your projects independently. This is important for keeping your development environment clean and organized. Here's how to manage virtual environments with Sublime Text:

  1. First, make sure you have Python's venv module available. If not, you'll need to install it using a package manager like pip.
  2. Create a virtual environment for your project:
 
python -m venv myenv
  1. Activate the virtual environment. On Windows, use:
 
myenv\Scripts\activate
  1. On macOS/Linux, use:
 
source myenv/bin/activate
  1. Install your project's dependencies in the virtual environment:
 
pip install -r requirements.txt
  1. In Sublime Text, you can configure the path to your Python interpreter in the virtual environment as shown in the "Syntax Specific Settings" section above.

Step 6: Additional tips and best practices

Consider the following tips to get the most out of Sublime Text:

Organize your project

Keep your project organized by using a clear folder structure. Separate source files, tests, and other components into different folders.

Use comments effectively

Comments help explain your code and are beneficial to both you and others who read your code later. Use the keyboard shortcut Ctrl + / (Windows/Linux) or Cmd + / (macOS) to toggle comments in your code.

Update packages regularly

Make sure you have the latest versions of packages and dependencies. You can update installed packages using the Anaconda package management system or the Sublime Text console.

View additional packages

In addition to the required plugins, you can search for and install additional packages from Package Control that can meet your specific needs such as Git integration, advanced code search, etc.

Conclusion

Setting up Sublime Text for Python development involves installing the software, configuring it with the necessary plugins, customizing preferences, and integrating it with your development workflow through virtual environments and key bindings. By following the steps outlined in this guide, you should be well-equipped to effectively handle Python projects in Sublime Text. Customize these settings and plugins to your specific needs and continue to explore further customizations as you move forward in your Python development journey.

If you find anything wrong with the article content, you can


Comments