usort
Safe, minimal import sorting
What it is and what it does
μsort is a Python import sorter designed to make minimal, safe changes to code. Rather than reordering all imports globally, it detects distinct blocks of imports separated by non-import statements and sorts only within those blocks. This conservative approach avoids the risk of breaking code where import order matters due to side effects or dependencies between imports.
Within each block, μsort groups imports by source (standard library, third-party, first-party, relative) and sorts lexicographically. It respects skip directives like `#usort:skip` and `#isort:skip` to exclude specific imports from reordering. The tool integrates with pre-commit workflows via a standard hook.
Use it for:
- Enforce consistent import ordering in CI pipelines using `usort check` without modifying code
- Automatically sort imports in files or directories in-place with `usort format` before committing
- Generate diffs of import changes with `usort diff` to review before applying
- Integrate import sorting into pre-commit hooks to maintain order on every commit
- Sort imports safely in codebases where import order affects behavior due to side effects
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
μsort sorts Python imports within detected blocks while preserving code safety by avoiding reordering across statements that separate import groups.
Yes. μsort is actively maintained, has no known vulnerabilities, installs with low friction, and solves a real problem—safe import sorting that respects code structure. It is a good fit if you want conservative import reordering that avoids breaking changes. The MIT license poses no restrictions.
Install
usort on PyPI
pip
pip install usortuv
uv add usortpoetry
poetry add usortInstalling usort
Before you install
Low friction installation with seven runtime dependencies. Active maintenance with latest release on 2026-01-13 and repository last commit on 2026-03-01.
License in practice
MIT license permits free use, modification, and distribution with minimal restrictions—suitable for both open-source and commercial projects.
Quickstart
pip install usort
usort format path/to/file.py
# or for validation:
usort check path/to/file.py
Requires Python 3.10 or newer to run
Verify before relying
- How μsort handles complex import patterns (star imports, conditional imports, or dynamic imports)
- Performance characteristics when sorting large files or projects with many imports
- Compatibility with other formatters and linters beyond pre-commit integration
Package facts
| License | MIT License Copyright (c) Meta Platforms, Inc. and affiliates. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the… (full text in the JSON record) (permissive) |
| Python support | supports the current Python release (>=3.10) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 7 — attrs, click, libcst, moreorless, stdlibs, tomli, trailrunner |
| Maintenance | actively maintained — 213 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 2,519,103/month — #3,026 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: usort-1.1.3-py3-none-any.whl
Keywords: imports, isort, usort
Tags
More Software Development packages
Provides backported and experimental type hints…
permissive · top 100 on PyPI
numpyNumPy provides an N-dimensional array object…
permissive · top 100 on PyPI
fastapiFastAPI is a Python web framework for building…
permissive · top 100 on PyPI
annotated-docProvides a way to document function parameters,…
permissive · top 100 on PyPI
typerTyper builds command-line applications from…
permissive · top 1,000 on PyPI
distlibDistlib provides low-level packaging utilities…
permissive · top 1,000 on PyPI
ufmtufmt combines black and µsort into a single…
permissive · top 5,000 on PyPI
reorder-python-importsAutomatically reorders Python imports into a…
permissive · top 15,000 on PyPI
classify-importsParses, classifies, and sorts Python import…
permissive · top 15,000 on PyPI
isortisort sorts Python imports alphabetically and…
permissive · top 1,000 on PyPI
pytest-isortA pytest plugin that automatically checks…
permissive · top 15,000 on PyPI
natsortnatsort provides natural sorting for strings…
permissive · top 1,000 on PyPI
aspy.refactor-importsParses, classifies, and manipulates Python…
permissive · top 15,000 on PyPI
sort-linesAlphabetizes lines in files based on…
permissive · top 15,000 on PyPI
csvsortSorts large CSV files on disk using a…
copyleft · top 15,000 on PyPI
stdlibsProvides a static listing of all known modules…
permissive · top 5,000 on PyPI