Package: importanize
Utility for organizing Python imports using PEP8 or custom rulesFree software: MIT licenseGitHub:https://github.com/miki725/importanizeInstallingYou can installimportanizeusing pip:$ pip install importanizeWhy?I think imports are important in Python. I also think PEP8 is awesome
(if you disagree, read some PHP) and there are many tools to help
developers reformat code to match PEP8. There are however fewer tools
for organizing imports either by following PEP8 or custom rules.
There isisort(which unfortunately I found out about after writing this lib)
however it seems to do lots of magic to determine which packages
are 3rd party, local packages, etc. I wanted the imports configuration
to be simple and explicit.
This is whereimportanizecomes in. It allows to organize
Python imports using PEP8 or your custom rules. Read on for
more information.UsingUsingimportanizeis super easy. Just run:$ importanize file_to_organize.pyThat will re-format all imports in the given file.
As part of the default configuration,importanizewill try
it’s best to organize imports to follow PEP8 however that is a rather
challenging task, since it is difficult to determine all import groups
as suggested byPEP8:standard library importsrelated third party importslocal application/library specific importsTo helpimportanizedistinguish between different import groups in most
cases it would be recommended to use custom config file:$ importanize file_to_organize.py config.jsonConfig file is simply ajsonfile like this:{
    "formatter": "grouped",
    "groups": [
        {
            "type": "stdlib"
        },
        {
            "type": "sitepackages"
        },
        {
            "type": "remainder"
        },
        {
            "type": "packages",
            "packages": [
                "my_favorite_package"
            ]
        },
        {
            "type": "local"
        }
    ]
}Default config looks something like:{
    "groups": [
        {
            "type": "stdlib"
        },
        {
            "type": "sitepackages"
        },
        {
            "type": "remainder"
        },
        {
            "type": "local"
        }
    ]
}Currently the only required key is"groups"which must be an array
of group definitions.importanizewill use these group definitions
to organize imports and will output import groups in the same order
as defined in the config file. These are the supported group types:stdlib- standard library imports including__future__sitepackages- imports coming from thesite-packagesdirectorylocal- local imports which start with".". for examplefrom .foo import barpackages- if this group is specified, additional keypackagesis required within import group definition which should list
all Python packages (root level) which should be included in that group:{
    "type": "packages",
    "packages": ["foo", "bar"]
}remaining- all remaining imports which did not satisfy requirements
of all other groups will go to this group.You can use the config file by specifying it in theimportanizecommand as shown above however you can also create an.importanizercfile and commit that to your repository. As a matter of fact,
you can see the.importanizercconfig file used for the importanize repository itself.
Additionally multiple configurations are supported within a single repository
via sub-configurations. Simply place.importanizercwithin a sub-folder
and all imports will be reconfigured under that folder.You can also choose the formatter used to organize long multiline imports.
Currently, there are two formatters available:grouped(default)inline-groupedIt can be set using the formatter config value, or the formatter option, for
example:$ importanize --formatter=inline-group --print tests/test_data/input.txtFinally, you can see all other availableimportanizecli options:$ importanize --helpNot all configurations can be provided via cli.
Additional available configurations in configuration file:length- line length after which the formatter will split importsexclude- list of glob patterns of files which should be excluded from organizing.
For example:"exclude": [
    "path/to/file",
    "path/to/files/ignore_*.py"
]after_imports_new_lines- number of lines to be included after importsadd_imports- list of imports to add to every file.
For example:"add_imports": [
    "from __future__ import absolute_import, print_function, unicode_literals"
]It integrates withpre-commit. You can use the following configrepos:
- repo: https://github.com/miki725/importanize/
  rev: 'master'
  hooks:
  - id: importanize
    args: [--verbose]ExampleHere is a before and after using the default formatter(on hypothetical file):Beforefrom __future__ import unicode_literals, print_function
import os.path as ospath
import datetime
from package.subpackage.module.submodule import CONSTANT, Klass, foo, bar, rainbows
from .module import foo, bar
from ..othermodule import rainbowsAfterfrom __future__ import print_function, unicode_literals
import datetime
from os import path as ospath

from package.subpackage.module.submodule import (
    CONSTANT,
    Klass,
    bar,
    foo,
    rainbows,
)

from ..othermodule import rainbows
from .module import bar, fooHere is whatimportanizedid:alphabetical sort, even inside import line (look at__future__)normalizedimport .. as ..intofrom .. import .. as ..broke long import (>80 chars) which has more than one import
into multiple linesreordered some imports (e.g. local imports..should be before.)TestingTo run the tests you need to install testing requirements first:$ make installThen to run tests, you can usenosetestsor simply use Makefile command:$ nosetests -sv
# or
$ make testHistory0.7.0 (2018-06-06)Fixed removing first line in files without imports.Added--listoption to list all found imports grouped by same packages as in config.0.6.4 (2018-05-29)Added support for custom line length.0.6.3 (2018-01-27)Fixed (again) importanize hanging when provided relative file path when finding sub-configurations.0.6.2 (2018-01-08)Fixed importanize failing on empty files.
ThanksMilind.Fixed importanize hanging when provided relative file path when finding sub-configurations.
ThanksMilind.0.6.1 (2017-10-06)Fixed sub-configrations. They are searched when organizing individual files now.0.6.0 (2017-10-06)Added support for sub-configurations when.importanizeis found.Added support foradd_importsin configuration.0.5.3 (2017-06-06)Added support to customize number of new lines added after imports
viaafter_imports_new_linesconfiguration.
Useful when using auto formatters such asyapf.0.5.2 (2017-05-18)Skipping directories which makes skipping subfolders much fasterFixed bug which incorrectly skipped files0.5.1 (2017-05-09)Fixed bug which incorrectly removed duplicate leafless imports which had differentasnames0.5.0 (2017-05-03)Added--ciflag to validate import organization in filesAddedsitepackagesimport group. ThanksPamela.
SeeREADMEfor more infoAdded pipe handling (e.g.cat foo.py | importanize)Fixed bug which incorrectly sorted imports with aliases (e.g.import foo as bar)Files are not overridden when imports are already organized.
Useful in precommit hooks which detect changed files.Released as Pythonwheel0.4.1 (2015-07-28)Fixed a bug whereimportanizedid not correctly detect stdlibs on Windows
(see#29)Removedfuturedependency sincesix>=1.9includes all the used featuresFixed tests to be executable on Windows0.4 (2015-04-13)Added multiple formatter options. Can be used using--formatterflag or can be set in the configuration file.Fixes a bug in parsing imports when encountering both\and()(see#26for example)Fixes a bug where wildcard leaf imports were combined with other others
(see#25for example)0.3 (2015-01-18)Using tokens to parse Python files. As a result this allows to
fix how comments are handled
(see#21for example)0.2 (2014-10-30)New “exclude” config which allows to skip filesPresetving origin file new line charactersTraversing parent paths to find importanize config file0.1.4 (2014-10-12)Multiple imports (e.g.import a, b) are normalized
instead of exitingMultiple imports with the same stem are combined into
single import statement
(see#17for example)0.1.3 (2014-09-15)Fixed where single line triple-quote docstrings would cause
none of the imports to be recognized0.1.2 (2014-09-15)Fixed where import leafs were not properly sorted for
mixed case (aka CamelCase)0.1.1 (2014-09-07)Ignoring comment blocks when parsing for importsFixed bug when imports start on a first line,
extra lines were being added to the file.0.1.0 (2014-09-07)First release on PyPI.CreditsDevelopment LeadMiroslav Shubernetskiy  -https://github.com/miki725ContributorsBenjamin Abel  -https://github.com/benjaminabelPamela McA’Nulty -https://github.com/PamelaMMilind Shakya -https://github.com/milinSerkan Hosca -https://github.com/shoscaLicenseThe MIT License (MIT)Copyright (c) 2014, Miroslav ShubernetskiyPermission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.