A CLI for sorting imports at the top of a file
Project description
Python Import Sorter
Overview
python-import-sorter is designed to automatically sort and format import statements of Python code.
It groups imports based on predefined or custom categories and ensures a clean, structured order for better code readability and maintainability.
Features
- Sorts
importandfrom ... import ...statements by line length first, then alphabetically - Groups imports based on predefined or custom categories
- Supports post-sorting formatting using external tools
- Works in-place or with standard input/output (
stdin/stdout)
Installation
Ensure you have Python 3.11+ installed.
You may also want to install a formatter like ruff for formatting:
pip install ruff
To install python-import-sorter directly from the repository:
pip install git+https://github.com/AntiMach/python-import-sorter
Usage
Run the tool via Python:
python -m import_sorter <file>
Or, if installed via pip, use the CLI directly:
import-sorter <file>
Options
-g, --groups: Define custom import groups (can be used multiple times)-f, --format: Specify a formatter to run after sorting (optional)-c, --config: Path to a config file with predefined arguments
Example:
import-sorter my_script.py -g numpy pandas -f "ruff format -"
Configuration Priority
Arguments can be provided in several ways. The priority is as follows (highest to lowest):
- Command-line arguments (
--groups,--format, etc.) - Arguments from the file specified with
--config import-sorter.tomlin the current working directory.[tool.import-sorter]section ofpyproject.tomlat the current working directory.
Contributions
Feel free to contribute by submitting pull requests or reporting issues.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file import_sorter-1.1.1.tar.gz.
File metadata
- Download URL: import_sorter-1.1.1.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4e2d0dbd9eae40a39ec733684cb725d9a3cc253e96afeb2a545352c39abcbb0c
|
|
| MD5 |
5a726fd9b734b56989455f8c9caaef6a
|
|
| BLAKE2b-256 |
2afb3d9c093eac0aefe785213c0d8a7acd38c14c449a4f3a42fafbcd28bca0a0
|
File details
Details for the file import_sorter-1.1.1-py3-none-any.whl.
File metadata
- Download URL: import_sorter-1.1.1-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
16b7b09775d39eccdbc58db70aa268fa9576f19920541850b33a787955e67ad1
|
|
| MD5 |
b877d50345957a4b1af697465820088a
|
|
| BLAKE2b-256 |
402b263d8784c509e73dd97b100db1cc213231907a7e45d13499c054f1819a07
|