usort is a command-line utility for Python projects that sorts import statements while trying to avoid risky rewrites.
Instead of aggressively reformatting a file, it detects import blocks that are likely to be safely interchangeable, reorders imports only within those blocks, and preserves the surrounding formatting. It’s designed to work alongside linters and code formatters rather than replace them.
This is free and open source software.
Key Features
- Sorts Python imports using a safety-first approach based on detected import blocks.
- Groups imports by standard library, third-party, first-party, and relative modules, then sorts them lexicographically within each group.
- Provides command-line modes to format files in place, show diffs, or check formatting for CI workflows.
- Supports pre-commit integration to enforce sorted imports before commits.
- Recognizes
#usort:skipand#isort:skipdirectives so selected imports remain untouched. - Works with Python source files and type stub files through its pre-commit hook definition.
Website: github.com/facebook/usort
Support:
Developer: Meta
License: MIT License
usort is written in Python. Learn Python with our recommended free books and free tutorials.
Related Software
| Python Code Formatters | |
|---|---|
| YAPF | Python formatter based on clang-format |
| Black | Uncompromising Python code formatter |
| isort | Python utility and library to sort imports |
| autopep8 | Formats Python code to conform to the PEP 8 style guide |
| blue | Somewhat less uncompromising code formatter than black |
| eradicate | Remove commented-out code from Python files |
Read our verdict in the software roundup.
Explore our comprehensive directory of recommended free and open source software. Our carefully curated collection spans every major software category.This directory is part of our ongoing series of informative articles for Linux enthusiasts. It features hundreds of detailed reviews, along with open source alternatives to proprietary solutions from major corporations such as Google, Microsoft, Apple, Adobe, IBM, Cisco, Oracle, and Autodesk. You’ll also find interesting projects to try, hardware coverage, free programming books and tutorials, and much more. Discovered a useful open source Linux program that we haven’t covered yet? Let us know by completing this form. |

