isort
isort is a command-line utility and library for Python that automatically sorts and organizes import statements into a consistent, configurable order.
Installation and Setup
Install it from the Python Package Index (PyPI) into a virtual environment:
A common setup that aligns with Black is:
pyproject.toml
[tool.isort]
profile = "black"
line_length = 88
known_first_party = ["package_name"]
src_paths = ["src"]
Key Features
- Groups and orders imports by section, such as standard-library, third-party, and local modules.
- Enforces consistent formatting of single and multi line imports and can combine or split imports as configured.
- Reads settings from
pyproject.toml,setup.cfg, or.isort.cfg, with ready made profiles likeblack. - Provides a check mode and clear exit codes for continuous integration and pre-commit workflows.
- Supports per-file ignores and skip patterns to fine-tune what gets formatted.
Usage
Sort imports in a file or directory in place:
$ python -m isort path/to/file.py
$ python -m isort src/ tests/
Check whether imports are correctly sorted without writing changes:
$ python -m isort --check --diff .
Override or fine-tune behavior on the command line:
$ python -m isort --profile black --line-length 100 .
Related Resources
Course
Absolute vs Relative Imports in Python
If you've worked on a Python project that has more than one file, chances are you've had to use an import statement before. In this course, you'll not only cover the pros and cons of absolute and relative imports but also learn about the best practices for writing import statements.
By Leodanis Pozo Ramos • Updated May 28, 2026