python

An often ignored but important part of the “Style Guide for Python Code” (PEP-8) is the Imports section.

This part on grouping imports is particularly important:

Imports should be grouped in the following order:

  • Standard library imports.
  • Related third party imports.
  • Local application/library specific imports.

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

The first group: “Standard library imports”

These are from the Python’s built-in standard library.

from typing import Any
import os

These are imports from installed packages. The PEP says “third party” which confuses a lot of people. To me it could be a library maintained by yourself or the same organisation, but is kept in a separate repository and installed as a separate package.

from airflow.utils.decorators import apply_defaults
from cx_Oracle import NUMBER

The third group: “Local application/library specific imports”

I take this to mean “imports from the same repository”.

from .stored_procedure_operator import StoredProcedureOperator

Putting it together

Separate each group by a blank line.

from typing import Any
import os

from airflow.utils.decorators import apply_defaults
from cx_Oracle import NUMBER

from .stored_procedure_operator import StoredProcedureOperator

Within each group, sort the lines alphabetically (this is just my preference, not part of the style guide).