83

I am interested in knowing what is the Python convention for newlines between the program parts? For example, consider this:

import os

def func1():

def func2():

What should be the ideal newline separation between:

  1. The import modules and the functions?
  2. The functions themselves?

I have read PEP8, but I wanted to confirm the above two points.

user225312
  • 126,773
  • 69
  • 172
  • 181
  • What modules in the standard library did you read? Many are excellent examples of Python coding style. Which ones did you examine? – S.Lott Jun 01 '10 at 21:09
  • 3
    I am going through os.py and I see that a single new line is used. That is why I asked. – user225312 Jun 01 '10 at 21:20
  • Direct link to the [Section about Blank Lines in PEP8](https://www.python.org/dev/peps/pep-0008/#blank-lines) – Martin Thoma Feb 12 '19 at 09:26
  • There's content on this all over the place but I can't seem to find any convention on how many lines between imports and global constants that follow. Are there two blank lines after imports and then two blank lines again after constants or ...?? – NeilG Dec 05 '22 at 06:59

3 Answers3

109
  1. Two blank lines between the import statements and other code.
  2. Two blank lines between each function.
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 22
    Just a minor clarification. It just so happens that in the above case you put 2 blank lines after imports and that is due to the functions. PEP8 says you have to surround top level functions with 2 lines, however if you were to have a constant/global variable there, instead of those functions it could have easily been 1 line. The most important thing to keep from my point is that there isn't such thing as to ALWAYS do a thing, especially when it comes to style, which is a subjective topic but what you should ALWAYS do is BE CONSISTENT with the project that you're working on. – Marius Mucenicu Apr 09 '18 at 10:16
  • 6
    As of Feb-2022, PEP8 actually states "Surround top-level function and class definitions with two blank lines", and "Method definitions inside a class are surrounded by a single blank line." – Xerxes Feb 09 '22 at 07:39
87

If one will check with 'Blank Lines' section of PEP8 — one will find the following:

Surround top-level function and class definitions with two blank lines.

Method definitions inside a class are surrounded by a single blank line.

Extra blank lines may be used (sparingly) to separate groups of related functions. Blank lines may be omitted between a bunch of related one-liners (e.g. a set of dummy implementations).

Use blank lines in functions, sparingly, to indicate logical sections.

Regarding imports, PEP8 states:

Imports should usually be on separate lines

...

Imports should be grouped in the following order:

  1. standard library imports
  2. related third party imports
  3. local application/library specific imports

You should put a blank line between each group of imports.

So, for your example, PEP8 compliant formatting would be:

import os


def func1():


def func2():

Just to give a more comprehensive illustration:

import re
import glob
import sys

import requests
import scrapy

from flask import Flask
from my_local_module import MyClass


def top_level_function1():
    pass


def top_level_function2():
    pass


class TestClass(object):
    
    def class_method1():
        pass

    def class_method2():
        pass


class TestClass2(object):
    
    def class2_method1():
        pass

    def class2_method2():
        pass
Community
  • 1
  • 1
3

Perfectly explained by user8554766

Just a simple modification

#Standard system imports
import re
import glob
import sys

#Related third party imports
import requests
import scrapy
from flask import Flask

#Local application/library specific imports
from my_local_module import MyClass1, MyClass

Reference

shellbot97
  • 198
  • 1
  • 12