In this tutorial we will explore how to automatically sort your Python module imports using isort library.
Table of Contents
As your Python projects grow, you start having more and more files and each file has more lines of code, performing more operations, and you import more dependencies.
In research step we usually import the libraries one by one as they are needed, making the entire imports section unorganized and often difficult to edit quickly.
In addition, when working in a team of engineers, most people have their own preferred way of structuring and ordering imports, which results in different file versions overwriting each other in the same repository.
This can be easily fixed using isort, which provides a systematic way of ordering imports in your Python project.
To continue following this tutorial we will need the following Python library: isort.
If you don’t have it installed, please open “Command Prompt” (on Windows) and install it using the following code:
pip install isort
What is isort
isort is a Python utility and a library that automatically sorts the Python module imports in alphabetical order while separating it into different section and by type.
In addition to a CLI utility and a Python library it has plugins for many code editors like VS Code, Sublime, and more.
Sample code file
In order to test the capabilities of isort library we will need a sample Python file to work with.
In this sample file we will mix up the order of imports and add some spacing to illustrate the difference between the unsorted and sorted files.
Here is a sample unsorted Python code (main.py):
import pandas import os import sys import numpy from sklearn.linear_model import LinearRegression from sklearn.linear_model import Ridge from sklearn.linear_model import ElasticNet
How to sort module imports
Once we have the Python file or files in a directory, it is easy to sort the module imports with isort.
Open the command line or terminal and navigate to the directory with the Python file(s).
Sort module imports in a single Python file
If you only have one file for which you would like to sort the module imports (in our case it’s main.py), simply run:
and the reformatted sample Python file should look like this:
import os import sys import numpy import pandas from sklearn.linear_model import ElasticNet, LinearRegression, Ridge
Looks much better and all the module imports are sorted and organized!
Sort module imports in multiple Python files
If you want to sort the module imports in multiple Python files or the entire Python project, simply run:
isort will automatically find all the Python files and sort the module imports in all the Python files in the directory.
In this article we explored how to automatically sort the module imports in Python files using isort library.
Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Python Programming tutorials.
The post Automatically Sort Python Module Imports using isort appeared first on PyShark.