isort
isort is a command-line utility and library for Python that automatically sorts and organizes import statements into a consistent, configurable order. By default, it groups them into standard-library, third-party, and local sections, following the import grouping recommended by PEP 8.
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, following the import grouping recommended by PEP 8.
- 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
Tutorial
Python Code Quality: Best Practices and Tools
Python best practices for code quality: what makes code good, and the tools and strategies that keep it that way as a project grows.
For additional information on related topics, take a look at the following resources:
- How to Manage Python Projects With pyproject.toml (Tutorial)
- Absolute vs Relative Imports in Python (Course)
- Managing and Measuring Python Code Quality (Course)
- Python Code Quality: Best Practices and Tools (Quiz)
- Everyday Project Packaging With pyproject.toml (Course)
- How to Manage Python Projects With pyproject.toml (Quiz)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 1, 2026