Python风格指南建议像这样对导入进行分组:
导入应按以下顺序分组:
- 标准库导入
- 相关第三方进口
- 本地应用程序/库特定的导入
然而,它并没有提到两种不同的进口方式应该如何布局:
from foo import bar
import foo
有多种方法对它们进行排序(让我们假设所有这些导入都属于同一个组):
先from..import
,然后import
from g import gg
from x import xx
import abc
import def
import x
first import
, then from..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.