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

Viewed 716

I have flask project and the structure of my init.py at the highest level is like:

import flask
from flask_sqlalchemy import SQLAlchemy

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

However I have a warning from pylint:

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

If I move that import to the top of the module, it fails. So, I would like to avoid that warning, maybe add it to exceptions. Could somebody advice, how to deal with such exceptions?

2 Answers

To avoid Pylint warnings on VSCode go to
Preferences > Settings > open any settings.json file and add

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

I don't know how to do the same on other editors.

To disable a particular warning remove "--errors-only" and add

"--disable=" + the warning you want to disable

check How do I disable pylint unused import error messages in vs code for more info.
I hope this can help you.

Another way to do it is by appending

#pylint: disable=wrong-import-position

but obviously, it avoids the warning only for that module

A way to provide a narrowly-focused override to Pylint's opinions is to append # noqa to the end of the line that Pylint is complaining about. E.g.,

from . import models  # noqa
Related