Sean
Sean

Reputation: 3385

Python blank line convention among import statements

I was writing some code in Python when I suddenly became curious regarding blank line conventions for import statements.

I'm aware of the typical import statement conventions specified in the PEP 8 style guide and for blank lines as well. However, I became curious if there is a convention or unwritten rule for blank lines among import statements.

For example, I usually like to put a blank line in between the three categories that are specified in PEP 8 (i.e. standard library imports, related third party imports, local application/library specific imports) but I've also noticed that many people tend not to do so. My PyLint application even throws a warning whenever I put a blank line.

I personally felt that this added a bit of clarity as to what "category" each imported library falls into. Is there a sort of convention that I should be following?

Thanks in advance.

Upvotes: 1

Views: 925

Answers (2)

askaroni
askaroni

Reputation: 993

Yes. The convention is to separate the sections. http://github.com/timothycrosley/isort can help.

The sections might look like this.

from __future__ import absolute_import

import os
import sys

from third_party import (lib1, lib2, lib3, lib4, lib5, lib6, lib7, lib8,
                         lib9, lib10, lib11, lib12, lib13, lib14, lib15)

from my_lib import Object, Object2, Object3

Alternatively, another popular, but not universal, convention is to only import modules, not classes or functions, as suggested in the Google Python Style Guide.

from __future__ import absolute_import

import os
import sys

import third_party.module1
import third_party.module2

import my_lib

Upvotes: 1

instead of blank line use a comment line in between the imports specifying what categories they fall into... it brings more clarity and no warnings or errors will be raised

Upvotes: 1

Related Questions