2

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?

Alexander
  • 391
  • 1
  • 3
  • 10
  • "If I move that import to the top of the module, it fails." how does it fail? Please edit your question to include the entire text of the error traceback you get when you try to run it. – Josh Karpel Oct 31 '20 at 16:27

2 Answers2

1

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

Cristiano
  • 267
  • 2
  • 10
  • 1
    VSCode. forgot to specify. – Cristiano Oct 31 '20 at 16:36
  • It looks nice, but I would like to hide that particular warning, but continue receiving other types of warnings. Is it possible? – Alexander Oct 31 '20 at 16:40
  • 1
    you can also try to add `#pylint: disable=wrong-import-position` – Cristiano Oct 31 '20 at 17:23
  • 1
    if a solution worked, you should mark that solution as accepter and/or like it, because someone spent his time trying to help you. Also because other people can have the same problem and if there is an accepted solution they can find quickly what they were looking for – Cristiano Oct 31 '20 at 18:41
  • Thank you for help, it works! I added `#pylint: disable=wrong-import-position` and now pylint ignore that warnings in particular module. – Alexander Oct 31 '20 at 21:36
0

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
Dave W. Smith
  • 24,318
  • 4
  • 40
  • 46
  • It doesn't work for me. I tried: `import ics.routes # noqa`, `import ics.routes # noqa:wrong-import-position`, but still have warning from pylint. Adding `#pylint: disable=wrong-import-position` at the top of the module works. – Alexander Oct 31 '20 at 17:43