Import order coding standard

alecxe picture alecxe · Mar 28, 2014 · Viewed 35.9k times · Source

PEP8 suggests that:

Imports should be grouped in the following order:

  1. standard library imports
  2. related third party imports
  3. local application/library specific imports

You should put a blank line between each group of imports.

Is there a way to check if the standard is violated anywhere in the package using static code analysis tools, like pylint, pyflakes, pychecker, pep8?


Example of violation:

from my_package import my_module
from django.db import models
import os

Correct way to import:

import os

from django.db import models

from my_package import my_module

Answer

sbywater picture sbywater · Feb 5, 2016

The current version of pylint now does this, and reports it as error class C0411.