The python style guide suggests to group imports like this:
Imports should be grouped in the following order:
- standard library imports
- related third party imports
- local application/library specific imports
However, it does not mention anything how the two different ways of imports should be laid out:
from foo import bar
import foo
There are multiple ways to sort them (let’s assume all those import belong to the same group):
-
first
from..import
, thenimport
from g import gg from x import xx import abc import def import x
-
first
import
, thenfrom..import
import abc import def import x from g import gg from x import xx
-
alphabetic order by module name, ignoring the kind of import
import abc import def from g import gg import x from xx import xx
PEP8 does not mention the preferred order for this and the “cleanup imports” features some IDEs have probably just do whatever the developer of that feature preferred.
I’m looking for another PEP clarifying this or a relevant comment/email from the BDFL (or another Python core developer). Please don’t post subjective answers stating your own preference.