Skip to main content

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 import and from ... 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):

  1. Command-line arguments (--groups, --format, etc.)
  2. Arguments from the file specified with --config
  3. import-sorter.toml in the current working directory.
  4. [tool.import-sorter] section of pyproject.toml at the current working directory.

Contributions

Feel free to contribute by submitting pull requests or reporting issues.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

import_sorter-1.1.1.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

import_sorter-1.1.1-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

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

Hashes for import_sorter-1.1.1.tar.gz
Algorithm Hash digest
SHA256 4e2d0dbd9eae40a39ec733684cb725d9a3cc253e96afeb2a545352c39abcbb0c
MD5 5a726fd9b734b56989455f8c9caaef6a
BLAKE2b-256 2afb3d9c093eac0aefe785213c0d8a7acd38c14c449a4f3a42fafbcd28bca0a0

See more details on using hashes here.

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

Hashes for import_sorter-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 16b7b09775d39eccdbc58db70aa268fa9576f19920541850b33a787955e67ad1
MD5 b877d50345957a4b1af697465820088a
BLAKE2b-256 402b263d8784c509e73dd97b100db1cc213231907a7e45d13499c054f1819a07

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page