Ben Chuanlong Du's Blog

It is never too late to learn.

Sort Python Imports Using isort

Installation

isort can be installed with the following command.

pip3 install isort

Configuration

There are 2 recommened ways to configure isort. The first recommended way is to place a file named .isort.cfg at the root of your project. For example,

[settings]
line_length=120
force_to_top=file1.py,file2.py
skip=file3.py,file4.py
known_future_library=future,pies
known_standard_library=std,std2
known_third_party=randomthirdparty
known_first_party=mylib1,mylib2
indent='    '
multi_line_output=3
length_sort=1
forced_separate=django.contrib,django.utils
default_section=FIRSTPARTY
no_lines_before=LOCALFOLDER

The second way is to add your desired settings under a [tool.isort] section in the pyproject.toml file under the root of your project.

[tool.isort]
line_length=120
force_to_top=file1.py,file2.py
skip=file3.py,file4.py
known_future_library=future,pies
known_standard_library=std,std2
known_third_party=randomthirdparty
known_first_party=mylib1,mylib2
indent='    '
multi_line_output=3
length_sort=1
forced_separate=django.contrib,django.utils
default_section=FIRSTPARTY
no_lines_before=LOCALFOLDER

Force Sorting Within Sections

force_sort_within_sections=True

Skip Processing of Imports

Please refer to Skip Processing of Imports (outside of configuration) for details.

IDE Support

isort is integrated into the Python Plugin for Visual Studio Code.

In [ ]:

Comments