301

I have the following line in my header:

import config.logging_settings

This actually changes my Python logging settings, but Pylint thinks it is an unused import. I do not want to remove unused-import warnings in general, so is it possible to just ignore this one specific line?

I wouldn't mind having a .pylintrc for this project, so answers changing a configuration file will be accepted.

Otherwise, something like this will also be appreciated:

import config.logging_settings # pylint: disable-this-line-in-some-way
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156

5 Answers5

453

Pylint message control is documented in the Pylint manual:

Is it possible to locally disable a particular message?

Yes, this feature has been added in Pylint 0.11. This may be done by adding # pylint: disable=some-message,another-one at the desired block level or at the end of the desired line of code.

You can use the message code or the symbolic names.

For example,

def test():
    # Disable all the no-member violations in this function
    # pylint: disable=no-member
    ...
    # pylint: enable=no-member

apply to a specific line only:

global VAR  # pylint: disable=global-statement

or for less verbosity, disable the ONLY following line (pylint 2.10+):

# pylint: disable-next=global-statement
global VAR

Pylint's manual also has further examples.

There is a wiki that documents all Pylint messages and their codes.

Alonme
  • 1,364
  • 15
  • 28
jomo
  • 14,121
  • 4
  • 29
  • 30
  • 22
    I like [flake8](https://pypi.org/project/flake8/)s `# noqa`. I dont want to see so many comments in the code. `# nolint` would be even clearer. – Nils Lindemann Jun 03 '20 at 23:14
  • 6
    @NilsLindemann the downside of that is that you lose information about what messages you were avoiding. You might accidentally suppress a message you didn't mean to (perhaps due to future version changes or something). – Pierce Darragh Apr 23 '21 at 23:04
  • 3
    @PierceDarragh if this precision is needed one can append one or more error codes, or classes of error codes. For example, in `def foo() :return 1 is 1`, appending `# noqa: E203` will not report the whitespace before the `:`. Appending `# noqa: E2` will not report any whitespace related issues. But the use of `is` instead of `==` will still be reported. See [flake8s](https://cutt.ly/ev12uLp) and [pycodestyles](https://cutt.ly/Pv12bvm) error codes. [More Infos](https://cutt.ly/iv12XN0). – Nils Lindemann Apr 24 '21 at 17:44
108
import config.logging_settings # pylint: disable=W0611

That was simple and is specific for that line.

You can and should use the more readable form:

import config.logging_settings # pylint: disable=unused-import
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
  • 2
    Where do you get the message explicit symbol from? My linter (the one in spyder) doesn't report it and the wiki linked in jomo's answer doesn't list it either. – Joooeey May 15 '18 at 17:43
  • You mean `unused-import`? I think my linter gave me a warning when I used W0611, but its been >3yrs so I do not remember :/ Sorry – The Unfun Cat May 15 '18 at 19:26
  • 1
    This [Pylint help page](https://pylint.pycqa.org/en/latest/messages/messages_list.html) lists all the Pylint errors (and warnings, etc.) in both coded and named format. Just do a "find in page" and search for either the code or name. – Michael Geary Apr 03 '22 at 23:36
13

In addition to the accepted answer:

You can re-enable the error checking adding pylint: enable:SPECIFIC_ERROR

For example, I have this in my code:

import time
import datetime
import os
import sys
# pylint: disable=import-error
import serial
# pylint: enable=import-error

This way you can ignore a single error on a single line without having to disable checking that error in the whole file

Alejo Dev
  • 2,290
  • 4
  • 29
  • 45
  • Do you know if there is a way to just disable for a particular line without having to re-enable it again? – Paul Wildenhain Jan 21 '22 at 15:40
  • 1
    This was the simplest way I found. You can completly disable errors globally in the .pylintrc file – Alejo Dev Jan 22 '22 at 16:21
  • 8
    Put the `pylint` comment _at the end of the line_ that you want to disable instead of a line of its own. Then it takes effect only for that line. – Michael Geary Apr 03 '22 at 23:28
  • 5
    From pylint 2.10 one can use `pylint: disable-next=...` option (see the accepted answer) which has an effect on the very NEXT line only. – Jongwook Choi Sep 08 '22 at 19:47
4

Checkout the files in https://github.com/PyCQA/pylint/tree/master/pylint/checkers. I haven't found a better way to obtain the error name from a message than either Ctrl + F-ing those files or using the GitHub search feature:

If the message is "No name ... in module ...", use the search:

No name %r in module %r repo:PyCQA/pylint/tree/master path:/pylint/checkers

Or, to get fewer results:

"No name %r in module %r" repo:PyCQA/pylint/tree/master path:/pylint/checkers

GitHub will show you:

"E0611": (
    "No name %r in module %r",
    "no-name-in-module",
    "Used when a name cannot be found in a module.",

You can then do:

from collections import Sequence # pylint: disable=no-name-in-module
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
loxaxs
  • 2,149
  • 23
  • 27
  • 1
    you can find them all in the [docs](http://pylint.pycqa.org/en/latest/technical_reference/features.html) – Esteban Jan 21 '19 at 08:19
  • Thanks, it's much better than going through the source. I found two more mirrors for the pylint doc. Apparently, [readthedoc](https://pylint.readthedocs.io/en/latest/technical_reference/features.html) is the official mirror. The other one is [pylint.org](https://docs.pylint.org/en/latest/technical_reference/features.html) – loxaxs Jan 21 '19 at 22:54
3

I believe you're looking for...

import config.logging_settings  # @UnusedImport

Note the double space before the comment to avoid hitting other formatting warnings.

Also, depending on your IDE (if you're using one), there's probably an option to add the correct ignore rule (e.g., in Eclipse, pressing Ctrl + 1, while the cursor is over the warning, will auto-suggest @UnusedImport).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Basic
  • 26,321
  • 24
  • 115
  • 201
  • 2
    This might work for some IDEs, but did not work for `emacs/flycheck`. Thanks. Please keep the answer since it might help someone else. – The Unfun Cat Mar 03 '15 at 10:24
  • 2
    This doesn't work when running `pylint` or `flake8` from the command line. – Jacob Tomlinson Aug 19 '16 at 10:02
  • @JacobTomlinson Interesting, thanks. It's what Pydev accepts and I was under the impression that pyclipse simply shelled out to pylint. Must either be slightly customised or maybe eclipse is processing those directives and suppressing the output. – Basic Aug 19 '16 at 15:13