imporganizer

A Python import organizer


Keywords
development import organizer organizing
License
MIT
Install
pip install imporganizer==1.1.1

Documentation

Imporganizer

Imporganizer organizes any imports of Python files found in the start of the file, putting them in alphabetic order. Comments or docstrings in the first lines are not supported yet. This feature is planned.

Requisites

Your imports need to be formatted in these rules:

  • They must be at the start of the file (blank lines before the first import will be removed).
  • They must use forward-slash to define multi-line imports.
  • They must be separated from the rest of the file (Python code) by 2 empty lines.

GOOD:

from django.models import Model, \
    Manager

BAD:

from django.models import (Model,
    Manager)

Usage

Install using pip:

$ pip install imporganizer

Use with the command line (main usage):

$ imporganizer organize-this.py --third-party=package1,package2 --homemade=package3,package4

Update the standard packages list with:

$ imporganizer --update-builtin

You can't update the builtin list and organize a file with a single command, but this is a feature in our roadmap.

Output

They will be categorized by:

  • Standard: all packages included with the default distribution of Python.
  • Third party: all packages not standard and not internal to the project.
  • Homemade: dependencies, packages and modules internal to the project.

To be able to categorize your packages this way, you must inform which ones are third party and which ones are internal to the project. You do this by the command line:

imporganizer organize-this.py --third-party=package1,package2 --homemade=package3,package4

For an input like this:

from django.forms import Form, \
    CharField
from textwrap import dedent

from django.db.models import Model
import sys
import os
import internal

import another_internal


print('hey')

The output will be:

import os
import sys
from textwrap import dedent

from django.db.models import Model
from django.forms import CharField, \
    Form

import another_internal
import internal


print('hey')

It will assume a maximum line length of 79 characters.