isort

Python command line utility and library that sorts imports alphabetically and groups them into sections by type.

install· pip

isort is a Python import formatter. It sorts imports alphabetically, splits them into sections by type (for example __future__, standard library, third-party and first-party), and removes duplicates. It ships as a command line utility and a Python library, and editor plugins are listed on the project wiki. It requires Python 3.11+ to run, but it can format Python 2 code.

From the command line, isort file.py file2.py sorts specific files and isort . recurses through a directory. --diff shows proposed changes without applying them. --atomic applies changes only if they don't introduce syntax errors, and it is off by default. -c (--check-only) reports files with incorrectly sorted imports to stderr, which suits CI and Git pre-commit hooks. isort supports pre-commit officially and provides a Git hook function. From Python, isort.file("pythonfile.py") rewrites a file and isort.code("import b\nimport a\n") returns sorted code as a string.

Output is configurable. multi_line_output has 12 wrapping modes for from-imports that exceed line_length. indent accepts a number of spaces, a tab or a quoted string. include_trailing_comma controls trailing commas in parenthesized imports. balanced_wrapping (or -e) picks the line length that gives the most balanced grid. Custom sections and ordering are configurable, and the project provides a Black compatibility guide. A # isort:skip comment skips a single import, and isort:skip_file in a module docstring skips the whole file. isort can also add or remove imports across multiple files. It is aimed at Python developers and teams who want consistent import ordering enforced locally or in CI.

Installation

# via pip
pip install isort

Alternatives & related tools

add a badge to your README

Maintain isort? Show that it's featured on trmn. The badge links back to this page.

Featured on trmn
Markdown
[![Featured on trmn](https://www.trmn.sh/badge/isort.svg)](https://www.trmn.sh/tools/isort?utm_source=badge)
HTML
<a href="https://www.trmn.sh/tools/isort?utm_source=badge"><img src="https://www.trmn.sh/badge/isort.svg" alt="Featured on trmn" height="20"></a>