Skip to main content

flake8-import-order

Build Status

A flake8 and Pylama plugin that checks the ordering of your imports.

In general stdlib comes first, then 3rd party, then local packages, and that each group is individually alphabetized, see Configuration section for details.

It will not check anything else about the imports. Merely that they are grouped and ordered correctly.

This plugin is under somewhat active development and is heavily influenced by the personal preferences of the developers of cryptography. Expect seemingly random changes and configuration changes as we figure out how it should work.

Warnings

This package adds 3 new flake8 warnings

  • I100: Your import statements are in the wrong order.

  • I101: The names in your from import are in the wrong order.

  • I201: Missing newline between sections or imports.

Configuration

You will want to set the application-import-names option to a comma separated list of names that should be considered local to your application. These will be used to help categorise your import statements into the correct groups. Note that relative imports are always considered local.

You will want to set the application-package-names option to a comma separated list of names that should be considered local to your company or organisation, but which are obtained using some sort of package manager like Pip, Apt, or Yum. Typically, code representing the values listed in this option is located in a different repository than the code being developed. This option is only supported if using the appnexus or edited styles.

import-order-style controls what style the plugin follows (cryptography is the default):

  • cryptography - see an example

  • google - style described in Google Style Guidelines, see an example

  • smarkets - style as google only with import statements before from X import … statements, see an example

  • appnexus - style as google only with import statements for packages local to your company or organisation coming after import statements for third-party packages, see an example

  • edited - style as smarkets only with import statements for packages local to your company or organisation coming after import statements for third-party packages, see an example

  • pep8 - style that only enforces groups without enforcing the order within the groups

You can also add your own style by extending Style class.

Limitations

Currently these checks are limited to module scope imports only. Conditional imports in module scope will also be ignored.

Classification of an imported module is achieved by checking the module against a stdlib list and then if there is no match against the application-import-names list. (If using the appnexus or edited styles, also the application-package-names list.) Only if none of these lists contain the imported module will it be classified as third party.

I201 only checks that groups of imports are not consecutive and only takes into account the first line of each import statement. This means that multi-line from imports, comments between imports and so on may cause this error not to be raised correctly in all situations. This restriction is due to the data provided by the stdlib ast module.

Imported modules are classified as stdlib if the module is in a vendored list of stdlib modules. This list is based on the latest release of Python and hence the results can be misleading. This list is also the same for all Python versions because otherwise it would be impossible to write programs that work under both Python 2 and 3 and pass the import order check.

Extending styles

You can add your own style by extending flake8_import_order.styles.Style class. Here’s an example:

from flake8_import_order.styles import Cryptography


class ReversedCryptography(Cryptography):
    # Note that Cryptography is a subclass of Style.

    @staticmethod
    def sorted_names(names):
        return reversed(Cryptography.sorted_names(names))

To make flake8-import-order able to discover your extended style, you need to register it as flake8_import_order.styles using setuptools’ entry points mechanism:

# setup.py of your style package
setup(
    name='flake8-import-order-reversed-cryptography',
    ...,
    entry_points={
        'flake8_import_order.styles': [
            'reversed = reversedcryptography:ReversedCryptography',
            # 'reversed' is a style name.  You can pass it to
            # --import-order-style option
            # 'reversedcryptography:ReversedCryptography' is an import path
            # of your extended style class.
        ]
    }
)

Release files for flake8-import-order 0.12

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

Source distribution (sdist)

Source distribution for flake8-import-order 0.12
File Size Uploaded
flake8-import-order-0.12.tar.gz 17.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flake8-import-order 0.12
File Interpreter ABI Platform
flake8_import_order-0.12-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size:32.6 kB

Release files / flake8-import-order-0.12.tar.gz

Download URL flake8-import-order-0.12.tar.gz
Size 17.9 kB
Tags Source
SHA-256 checksum
How to use checksums
2ff36d2fe44ffacb2738fab3e33a98c71b07cc6e995af9a38b16d262da928856
BLAKE2b-256 checksum
How to use checksums
75024e83dd8502f18aaae88331f9a05a5c2ce034306a47bab59bf8205a1351e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / flake8_import_order-0.12-py2.py3-none-any.whl

Download URL flake8_import_order-0.12-py2.py3-none-any.whl
Size 14.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
2bb3278cdf2f5d9bff03105a85d6f011775656735872daadba1c4881872ba31f
BLAKE2b-256 checksum
How to use checksums
6fa39873a279c4cca791ab0e8fca694aa682bdd3297339254459cd6edfdc58bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.19.2

2 release files

0.19.1

2 release files

0.19.0

2 release files

0.18.2

2 release files

0.18

2 release files

0.17

2 release files

0.16

2 release files

0.15

2 release files

0.14.2

2 release files

0.14.1

2 release files

0.14

2 release files

0.13

2 release files

This release

0.12 This release

2 release files

0.11

2 release files

0.10

2 release files

0.9.2

2 release files

0.9.1

2 release files

0.9

2 release files

0.8

2 release files

0.7

2 release files

0.6.1

2 release files

0.6

3 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4

2 release files

0.3.2

2 release files

0.3.1

3 release files

0.3

1 release file

0.2

1 release file

0.1

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