# isort

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

Stars: 7.0k · Language: Python · License: MIT · Categories: Dev Tools

- Repository: https://github.com/PyCQA/isort
- Website: https://isort.readthedocs.io/
- Page: https://www.trmn.sh/tools/isort

## Install

- pip: `pip install isort`

## Overview

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

```bash
# via pip
pip install isort
```

## Alternatives

- [Grip](https://www.trmn.sh/tools/grip.md): Python command-line server that renders local Markdown readme files through the GitHub Markdown API, with live refresh and HTML export. (Python, 6.8k stars)
- [Hatch](https://www.trmn.sh/tools/hatch.md): Python project manager covering builds, environments, Python distributions, testing, static analysis, version management, and publishing to PyPI. (Python, 7.3k stars)
- [harlequin](https://www.trmn.sh/tools/harlequin.md): Terminal-based SQL IDE with database adapter support for DuckDB, SQLite, PostgreSQL, MySQL and dozens of other databases (Python, 6.5k stars)
- [Azure CLI](https://www.trmn.sh/tools/azure-cli.md): Microsoft's cross-platform command-line interface for managing Azure resources with az group/subgroup/command syntax, JMESPath queries, and tab completion. (Python, 4.7k stars)
- [tox](https://www.trmn.sh/tools/tox.md): Command-line virtual environment manager and test runner for Python that automates testing across environments and acts as a frontend to CI servers. (Python, 4.0k stars)

---

Canonical: https://www.trmn.sh/tools/isort
