111

I tried with

#:PEP8 -E223

or

# pep8: disable=E223

I thought the second would work but doesn't seems to work.

Do you have an idea how I can handle this ?

9 Answers 9

128

As far as I know, you can't. You can disable errors or warnings user wide, or per project. See the documentation.

Instead, you can use the # noqa comment at the end of a line, to skip that particular line (see patch 136). Of course, that would skip all PEP8 errors.

The main author argues against source file noise, so they suggested # pep8 comments don't get included.


Note that there is also nopep8, which is the equivalent. noqa (which stands for No Quality Assurance was added in version 1.4.1 to support people running pyflakes next to pep8.

1
  • 12
    Thanks. But in my case, I don't want to skip a specific line :-( To my mind, it is less poluting to add something like "# pep8: disable E221" at the begining of the file than specify each line to ignore. But it is my point ! Thanks for the answer.
    – Flows
    Commented Aug 27, 2013 at 7:24
51

Try putting # nopep8 at the end of the line (after two spaces). So if the line of code is:

h=1+2+3+4+5+6+func( "hello","world")

then to ignore the copious pep8 errors for that line it becomes:

h=1+2+3+4+5+6+func( "hello","world")  # nopep8
2
  • 1
    It seems that this can be combined with a specific error type, e.g. # nopep8: E501
    – Bilal Akil
    Commented May 17, 2022 at 7:07
  • 6
    the irony of making a line longer to say you want it ignored for length
    – Blundell
    Commented Sep 9, 2022 at 10:52
32

Let me add something that was probably introduced after all the previous answers were posted.

If you use Flake8, you can ignore a specific violation raised in a specific line, by adding

# noqa: F401

at the end of the line, where F401 here is an example of an error code. For a list of all violations code, see http://flake8.pycqa.org/en/3.5.0/user/error-codes.html and https://pycodestyle.readthedocs.io/en/latest/intro.html#error-codes

You can also ignore all violations in an entire file by adding

# flake8: noqa

anywhere in the file.

Reference: http://flake8.pycqa.org/en/3.5.0/user/violations.html

1
  • 1
    Thank you sir, this does not answer the precise question but it does come up in a search for what you answered and that is helpful :) Disabling at a line is what I and others who will arrive here needed!
    – rjurney
    Commented Aug 25, 2022 at 18:15
28

You can use --ignore flag to disable the error you mentioned above

pep8 --ignore=E223 file_name.py

for multiple errors

pep8 --ignore=E223,E501 file_name.py

For more in depth knowledge of other flags you can scan through http://pep8.readthedocs.org/en/latest/intro.html

1
  • 3
    This doesn't answer the question (while it may be useful).
    – Jeppe
    Commented Aug 16, 2020 at 14:27
3

You can do that using Flake8 together with https://github.com/jayvdb/flake8-putty

2
  • flake8-putty is definitely the best approach overall - cause it allows a much more granular control which things to disable for what files.
    – alecxe
    Commented Jun 22, 2017 at 16:34
  • Unfortunately not compatible with flake8 version 3 and no plans to fix: github.com/jayvdb/flake8-putty/issues/14
    – Carl
    Commented Mar 8, 2019 at 11:12
1

If you use Flake8 3.7.0+, you can ignore specific warnings for entire files using the --per-file-ignores option.

Command-line usage:

flake8 --per-file-ignores='project/__init__.py:F401,F403 setup.py:E121'

This can also be specified in a config file:

[flake8]
per-file-ignores =
    __init__.py: F401,F403
    setup.py: E121
    other/*: W9
1
  • this answers another question but not OP's
    – chichak
    Commented Jan 24 at 13:59
0

You can do that with, for example, your setup configuration file (setup.cfg):

[tool:pytest]
pep8ignore =
    *.py E501 W503
    api.py E402                <=============== HERE
    doc/* ALL
pep8maxlinelength = 120
flakes-ignore =
    UnusedImport
filterwarnings =
  ignore::DeprecationWarning
0

I am using "autopep8" and the mentioned answers didn't work for it. Instead, I had to do this to disable formatting for a specific line:

# autopep8: off
def hello(action, feature):
# autopep8: on
    ...

It is possible to disable autopep8 untill it it turned back on again in the file, using autopep8: off and then renabling autopep8: on.

Source

-1

In the IDE of LiClipse, you can add --ignore=E501 in Code Analysis from Preference.

If anyone uses Eclipse like IDE, this is the convenient way to do it.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.