Pylint Import "import routes" 应该放在模块的顶部

Pylint Import "import routes" should be placed at the top of the module

我有 Flask 项目,我的 init.py 的最高层结构如下:

import flask
from flask_sqlalchemy import SQLAlchemy

flask_app = flask.Flask(__name__)
db = SQLAlchemy(flask_app)
...
import routes
from . import models

但是我收到了来自 pylint 的警告:

Import "import routes" should be placed at the top of the module

如果我将该导入移动到模块的顶部,它会失败。所以,我想避免那个警告,也许将它添加到例外中。有人可以建议,如何处理此类异常?

要避免 VSCode 上的 Pylint 警告,请转到
首选项 > 设置 > 打开任何 settings.json 文件并添加

    "python.linting.pylintArgs": [
        "--errors-only"
    ],

我不知道如何在其他编辑器上做同样的事情。

要禁用特定警告,请删除“--errors-only”并添加

"--disable=" + 您要禁用的警告

查看 How do I disable pylint unused import error messages in vs code 获取更多信息。
我希望这可以帮助你。

另一种方法是附加

#pylint: disable=wrong-import-position

但显然,它避免了仅针对该模块的警告

提供 narrowly-focused 覆盖 Pylint 意见的一种方法是将 # noqa 附加到 Pylint 抱怨的行的末尾。例如,

from . import models  # noqa