python - Pylint 在以正确的顺序导入时提示

标签 python pylint

我认为Python中导入的正确顺序是问题的第一个答案所描述的顺序: What's the correct way to sort Python `import x` and `from x import y` statements?

因此,这段代码应该是正确的:

import os
import time
import yaml

from collections import OrderedDict
from xtesting.core import testcase

但是,当我运行 Pylint 时,我得到:

C:  5, 0: standard import "from collections import OrderedDict" should be placed before "import yaml" (wrong-import-order)

所以我猜“yaml”不是标准库。那么正确的方法应该是这个(即使它更难看并且可读性较差)吗?

import os
import time
from collections import OrderedDict
import yaml

from xtesting.core import testcase

最佳答案

PyYAML 不是标准 Python 库的一部分,从标准库导入,无论是通用的 (import os) 还是特定的 (from collections import OrderedDict) 都应该放在第一位.

IMO,您应该按字典顺序对各部分中的模块名称进行排序,并用空行分隔各部分:

from collections import OrderedDict
import os
import time

from xtesting.core import testcase
import yaml

有些人希望在每个部分中将通用的全部放在第一位:

import os
import time
from collections import OrderedDict

import yaml
from xtesting.core import testcase

这看起来更好,但在长通用列表之后更容易忽略特定的导入。它还将通用和特定导入与同一个模块分开,在我看来,这很糟糕:

import yaml
from xtesting.core import testcase
from yaml import safe_load

关于python - Pylint 在以正确的顺序导入时提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50630822/

相关文章:

python - Virtualenv 和 Anaconda 问题

vim - 如何在 Vim quickfix 中控制错误输出格式?

python - R1732 : Consider using 'with' for resource-allocating operations (consider-using-with)

python - `/bin/sh` 在 docker 内的 Jenkins 内的 tox 中未找到

python - 未使用的变量错误,gem5

python - Pyramid :是否可以使用授权而无需身份验证?

python - 物体和相机之间的关联 Blender python

python-3.x - Cloud 9 CS50 上的 pylint 错误消息

python - 当我稍后设置变量值时,如何避免来自 Pylint 的 "not subscriptable"类型检查?

python - 来自 Jenkins/Sonarqube 的 Pylint 错误