Skip to main content

Groups, lexicographically sorts and formats import statement into the three groups as defined by PEP8 (standard library, third-party packages, current package/application.)

Imports from the same package are collapsed into a single statement, and multiple identifiers from the same packages are grouped using parentheses and separated by a newline.

Usage

Usage: format-imports.py [options]

Groups, sorts, and formats import statements.

Options:
-h, --help            show this help message and exit
-a APPLICATION, --application=APPLICATION
-s STDLIB_FILES, --stdlib-file=STDLIB_FILES
                        File(s) containing additional module names to add to
                        the standard library set.

The script accepts input via stdin, and outputs the formatted code via stdout.

Example

For the given bad.py:

import sys

import foo
import bar as other
from baz import (
    package,
    package3,
)
from module import package3 as package4
from baz import package2, package1

import application
from application.utils import memoize
from application.models import User
from application.models import Place

import os

The output of format-imports.py -a application < bad.py will be:

import os
import sys

import bar as other
import foo
from baz import (
    package,
    package1,
    package2,
    package3,
)
from module import package3 as package4

import application
from application.models import (
    Place,
    User,
)
from application.utils import memoize

Usage with Vim

Select a chunk of text in visual mode, then invoke the format-imports.py script against the chunk with:

:'<,'>!format-imports.py -a <application>

Release files for importformatter 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for importformatter 0.1.3
File Size Uploaded
importformatter-0.1.3.tar.gz 4.2 kB Details

Release files / importformatter-0.1.3.tar.gz

Download URL importformatter-0.1.3.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
716fb335f5205d93d03794b8a15e6137c9b79e702230007b6e68bbfbdb5dd883
BLAKE2b-256 checksum
How to use checksums
5e3f29a5dac334012d513126458d18d7cbd576604eebc7e8f5e097bd7232bf67
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.3 This release

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page