导入语句应该各自独占一行. typing 和 collections.abc 的导入除外 例如: Yes:
from collections.abc import Mapping, Sequence
import os
import sys
from typing import Any, NewType
No:
import os, sys
导入总应该放在文件顶部, 位于模块注释和文档字符串之后, 模块全局变量和常量之前. 导入应该按照从最通用到最不通用的顺序分组: (1)标准库导入 (2)第三方库导入 (3)应用程序指定导入 每种分组中, 应该根据每个模块的完整包路径按字典序排序, 忽略大小写,如:
import foo
from foo import bar
from foo.bar import baz
from foo.bar import Quux
from Foob import ar
在每个分组内部, 应该按照模块完整包路径 (例如 from path import ...
中的 path
) 的字典序排序, 忽略大小写. 可以选择在分组之间插入空行.
import collections
import queue
import sys
from absl import app
from absl import flags
import bs4
import cryptography
import tensorflow as tf
from book.genres import scifi
from myproject.backend import huxley
from myproject.backend.hgwells import time_machine
from myproject.backend.state_machine import main_loop
from otherproject.ai import body
from otherproject.ai import mind
from otherproject.ai import soul
# 旧的代码可能会把这些导入语句放在下面这里:
#from myproject.backend.hgwells import time_machine
#from myproject.backend.state_machine import main_loop