Tabnine Live: Implement and validate Atlassian Jira issues with one click
October 10
8 am PT
Home / Blog /
How to configure Sublime Text 4 for Python development
//

How to configure Sublime Text 4 for Python development

//
Tabnine Team /
6 minutes /
September 5, 2021

What do you need to develop in Python? The first thing on your list should be an IDE, or at the very least a code editor. Sure, you can type your code in any old text editor, including notepad.exe. But why would you want to, knowing that code editors and IDEs can make your coding experience smoother and faster?

When you search the web for the best IDE for Python development, you will get a large selection of editors and IDEs at a varying cost and versatile feature-set. On most top Python code editors lists, you will find Sublime Text, a flexible open-source text editor with many community-made plugins (packages) to extend its functionality.

Why use Sublime Text 4 for Python Development?

The Sublime text editor is a code and text editor with a rich set of features to make your Python development (or development in most languages) seamless. Sublime Text is a lightweight code editor that, like VSCode and Atom, relies on plugins or packages to extend its functionality to a full-fledged IDE.

Being an open-source text editor, it has an avid community of developers to build different plugins that enable the IDE capabilities to the editor.

Keep yourself up to date on the latest developments and trends in software development.

In the rapidly changing field of software development, staying informed about new technologies and practices is crucial for success. Learn how to utilize AI and optimize your software engineering skills in 2023.

 

Setting up Sublime Text 4 for Python

To get started with Python on Sublime Text 4 (the latest version), you need to install a Sublime Text 4 and some plugins. So let’s get to it.

Download & Install Sublime Text 4

Download Sublime Text 4 from the downloads page. We’ll be downloading and installing the Windows version in this tutorial, but the process is similar to other supported operating systems.

Double click on the Installer you’ve downloaded to launch the installation wizard.

Choose the path to install Sublime Text 4 and click Next. You’ll be taken to the Additional Tasks window. 

Select the Add to explorer context menu option to enable the “Open with Sublime Text” option to the context menu that appears when you right-click on any file of a relevant type. 

Click Next. The wizard is now ready to install.

In the Ready to Install window, you’ll see the summary of the options you’ve selected. Click Install to Install the Sublime Text. 

Sublime Text 4 will be installed and you’ll see the complete installation window. 

Click Finish to complete the installation of the Sublime Text. Sublime Text 4 will be installed. 

Next, let’s configure the Sublime Text Editor for Python development with the appropriate package. But first, we need to install the component to let us install packages on Sublime Text 4.

Install Package Control

As mentioned previously, Sublime Text functionality can be extended using plugins called packages. To install, update and manage them, you will need to install Package Control on Sublime Text.

You can install package control by using the menu Tools -> Install Package Control option from within Sublime Text. 

Once the installation is done, you’ll see the message that the Package Control was successfully installed. 

Now you can use the Command Palette to manage packages. 

To install a package, Select Preferences -> Package Control menu. 

You’ll see the package control option. Type install and you’ll see the option for install as shown below.

Select the Install-Package option.

Now we can install the packages we need to support Python on Sublime Text 4.

It’s worth noting that this is also how you can set up other packages to customize Sublime Text to your needs. Of course, we recommend you consider Tabnine for Sublime Text for your predictive code completion needs.

Install LSP-pyslp

LSP (Language Server Protocol) can be used between source code editors such as Sublime Text and servers that provide programming language features. The protocol’s objective is to allow programming language support to be implemented and distributed independently of any given editor. This means using this protocol, any text editor can be configured for development in a specific language.

Sublime doesn’t natively support the LSPs. However, the community developed a plugin called LSP Project to call the different language servers and render the results in the editor. 

For our purposes, we’ll be setting up the LSP-pyslp package, a convenience package for the Python language servers. 

To install a package, open the package installer by selecting the menu Preferences -> Package Control and select the Install Package option. 

In the install package window, type LSP-pylsp and you’ll see the package. Select that to install the LSP-lsp package. 

LSP-pylsp will be installed and you’ll see the LSP option as shown below. 

Select the LSP-pyslp option to set the preferences of the LSP-pyslp package. 

This will open the LSP-pylsp.sublime-settings file. 

On the right-hand side, you’ll see the default settings file. On the left-hand side, you’ll see the option with the text “Settings in here override those in “LSP-pylsp/LSP-pylsp.sublime-settings”. This means you can use this section to customize the LSP-pyslp package.

We’ll add a few LSP plugins to the customization section of the file to enable some helpful features for Python development on Sublime Text.

  • Mypy-ls is a static type checker plugin that allows you to find errors in your program statically
  • Flake8 is a code linter plugin which checks your program for programmatic and syntax errors as you type
  • Pyls_black is a code formatting plugin that can be used to format the source code for better readability

To add these, you’ll need to enter the following in the customization section of the file:

{

    “pylsp.plugins.mypy-ls.enabled”: true,

    “pylsp.plugins.flake8.enabled”: true,

    “pylsp.configurationSources”: [“flake8”],

    “pylsp.plugins.pyls_black.enabled”: true,

}

After adding the configuration, Click File -> Save to save the configuration of the LSP package and close the settings file. 

Creating a Python File

Using Sublime Text for Python development is straightforward once you have the LSP package installed.

Launch the Sublime Text editor to open a new blank file. You can code a python program and save it as a sample.py file. 

For example:

# Python3 program to swap first

# and last element of a list

# Swap function

def swapList(newList):

size = len(newList)

# Swapping

temp = newList[0]

newList[0] = newList[size – 1]

newList[size – 1] = temp

return newList

# Driver code

newList = [12, 35, 9, 56, 24]

print(swapList(newList))

Now, let’s see if the LSP packages are correctly installed. 

Hover over the method name swapList, and you’ll see the definitions and references to this method, as shown below. 

The message box shows that the swapList method is referred to in the file sample.py and line number 18. This result indicates that the LSP package for Python is functioning as expected.

Running the Program

You can run the python program by using the menu Tools -> Build option. If the default build system is not configured, you can select Python in the Tools -> Build system -> Python to make Python the default build system.

Summary

Though many professional Python developers default to paid IDEs like PyCharm, for beginner developers or programming polyglots, Sublime Text Editor offers a flexible and lightweight cross-platform editor.