reorder-python-imports is a command-line utility that automatically reorganises import statements in Python source files. It uses static analysis to classify imports and apply a consistent structure, helping developers maintain readable and predictable source code.
Import statements can become disorganised as projects grow. Standard library modules, third-party dependencies and application-specific modules may be mixed together, while unused compatibility imports and duplicate statements accumulate over time.
reorder-python-imports addresses these issues by rewriting import statements according to defined conventions. Unlike a general code formatter, it focuses specifically on imports and their relationships to the surrounding project structure.
A particularly useful aspect of the utility is its approach to reducing merge conflicts. By placing individual imports on separate lines, developers can add or remove imported symbols without repeatedly modifying the same lines of code.
The utility also assists with modernising older Python projects by replacing compatibility imports with their standard library equivalents where appropriate.
This is free and open source software.
Key Features
- Automatically separates imports into standard library, third-party and application-specific groups.
- Uses static analysis to distinguish modules belonging to different import categories.
- Places regular import statements before from-import statements.
- Splits statements importing multiple symbols into individual import lines.
- Removes duplicate import statements.
- Alphabetical ordering within import groups to maintain consistent source organisation.
- Adds specified import statements to Python source files.
- Removes unwanted imports and replaces references to renamed or relocated modules.
- Automatically removes obsolete __future__ imports according to the selected Python compatibility target.
- Rewrites selected compatibility imports from six, mock and typing-related modules.
- Configurable application directories for projects using non-standard source layouts.
- Support for identifying application modules that cannot be classified through normal filesystem inspection.
- Special noreorder comments prevent selected imports from being modified.
- Integration with pre-commit for automated import organisation during development.
- Handles files individually and supports standard input and output for use in scripts.
Website: github.com/asottile/reorder-python-imports
Support:
Developer: Anthony Sottile
License: MIT License
reorder-python-imports is written in Python. Learn Python with our recommended free books and free tutorials.
Related Software
| Python Code Formatters | |
|---|---|
| Ruff | Extremely fast Python linter and code formatter |
| 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 |
| Darker | Apply black reformatting to Python files |
| Pyink | Python code formatter |
| blue | Somewhat less uncompromising code formatter than black |
| μsort | Sorts import statements |
| eradicate | Remove commented-out code from Python files |
Read our verdict in the software roundup.
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. |


Please read our Comment Policy before commenting.