Skip to main content

Groups, sorts, and formats import statements.

Project description

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>

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

importformatter-0.1.1.tar.gz (4.0 kB view details)

Uploaded Source

File details

Details for the file importformatter-0.1.1.tar.gz.

File metadata

File hashes

Hashes for importformatter-0.1.1.tar.gz
Algorithm Hash digest
SHA256 74fd9a0f7f2d648cd964bd9f3415f0437c013b43bdefd85ccd74ba5431c82487
MD5 8cb5aa5ca153e0d249e12b6513e3cbe0
BLAKE2b-256 9e90db377964dd51fb0e00f6d443ae6d7debf71dbd29200a9894f9e8900f5d01

See more details on using hashes here.

Supported by

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