μsort – sorts import statements

μsort (usort) is a Python import sorting utility designed to make minimal, predictable changes to source code. Its primary objective is to organise imports without introducing potentially unsafe transformations or unnecessarily altering existing formatting.

Unlike import sorters that may reorganise entire import sections, μsort identifies blocks of statements that can be safely reordered. Imports separated by executable statements or certain comments are treated as independent blocks, reducing the possibility of changing program behaviour.

The utility is particularly suitable for projects where preserving source layout and avoiding unnecessary changes are important. It is designed to work alongside general-purpose code formatters and linters rather than replace them.

μsort provides command-line operations for formatting imports, checking their order and reviewing proposed changes.

This is free and open source software.

Key Features

  • Automatically sorts Python imports using a safety-first approach.
  • Identifies independent blocks of imports that can be safely reordered.
  • Avoids moving imports across executable statements and other block boundaries.
  • Groups imports into standard library, third-party, first-party and relative categories.
  • Sorts imports lexicographically within their respective groups.
  • Preserves existing formatting wherever possible rather than rewriting import layouts.
  • Supports sorting imports in individual source files and directories.
  • Provides a format command to apply import ordering changes.
  • Offers diff mode for reviewing proposed changes without modifying files.
  • Includes check mode for validating import order in automated workflows.
  • Recognises #usort:skip directives for excluding specific imports from sorting.
  • Supports #isort:skip directives for compatibility with existing projects.
  • Allows configuration of import categories and sorting behaviour.
  • Integrates with pre-commit for automatic import checking and organisation.
  • Works alongside other Python code formatting and linting tools.

Website: github.com/meta/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
RuffExtremely fast Python linter and code formatter
YAPFPython formatter based on clang-format
BlackUncompromising Python code formatter
isortPython utility and library to sort imports
autopep8Formats Python code to conform to the PEP 8 style guide
DarkerApply black reformatting to Python files
PyinkPython code formatter
docformatterAutomatically formats Python docstrings for consistent style
SSortSorts Python statements by dependency for clearer code
reorder-python-importsReorders Python imports into a consistent, readable layout
μsortSorts import statements
eradicateRemove commented-out code from Python files

Read our verdict in the software roundup.


Best Free and Open Source Software LinuxLinks helps you discover the best free and open source software for Linux. Explore our extensive collection of recommended applications, including alternatives to popular proprietary software.

We also publish software reviews, Linux tutorials, hardware reviews, free programming books and articles covering the wider open source community. Browse our Linux resources to discover more.

Know a useful free and open source Linux application we haven't covered? Suggest a program for inclusion.

Has your software been featured on LinuxLinks? Display one of our Featured on LinuxLinks badges on your website or GitHub README.
Subscribe

Please read our Comment Policy before commenting.

Notify of
guest
0 Comments
Oldest
Newest Most Voted