PyCharm shows "PEP8: expected 2 blank lines, found 1"

87,834

Solution 1

Just add another line between your function definitions :

1 line :

enter image description here

2 lines:

enter image description here

Solution 2

This is a pretty common question within the python community. After the release of PEP 8, new formatting styles were accepted into python. One of them states that after the definition of a class or function there must be two lines separating them. As such:

    def yadayada:
     print("two lines between the functions")


    def secondyadayada:
     print("this is the proper formatting")

So, you should never do it like:

    def yadayada:
     print("two lines between the functions")

    def secondyadayada:
     print("this is the proper formatting")

Or else PyCharm will throw that error at you.

Share:
87,834
march_seven
Author by

march_seven

Updated on July 09, 2022

Comments

  • march_seven
    march_seven almost 2 years

    Consider the following code:

    def add_function(a, b):
        c = str(a) + b
        print "c is %s" % c
    
    def add_int_function(c, d):
        e = c + d
        print "the vaule of e is %d" % e
    
    if __name__ =="__main__":
        add_function(59906, 'kugrt5')
        add_int_function(1, 2)
    

    It always shows me: "expected 2 blank lines ,found 1" in aadd_int_function, but not in the add_function.

    When I add two spaces in front of the def add_int_function(c, d): there is a error shows unindent does not match any outer indentation level in the end of add_function:

    enter image description here

    enter image description here

  • Kennet Celeste
    Kennet Celeste almost 7 years
    @march_seven if this solved your issue you can accept the answer so that the others will immediately find out this solved the problem
  • Franz Deschler
    Franz Deschler over 6 years
    Why is this necessary? What happens if there is just 1 line between the functions?
  • TT--
    TT-- over 6 years
    @FranzDeschler nothing happens, it just does not conform to the style guide, python.org/dev/peps/pep-0008/#blank-lines
  • Leo Ufimtsev
    Leo Ufimtsev almost 5 years
    Is it possible to turn this pep8 behaviour off?
  • Alan Bagel
    Alan Bagel almost 3 years
    @Leo Ufimtsev Go to Settings -> Editor -> Inspections -> Python -> PEP 8 coding style violation. Then press the check mark to disable it. You can also change the severity if you want to.
  • Gulzar
    Gulzar over 2 years
    @grandBagel Some things are better left unknown