minhee
minhee

Reputation: 5818

PEP 8: How should __future__ imports be grouped?

According to PEP 8:

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.

But it does not mention about __future__ imports. Should __future__ imports be grouped together with standard library imports or separated from standard library imports.

So, which is more preferred:

from __future__ import absolute_import
import sys
import os.path

from .submod import xyz

or:

from __future__ import absolute_import

import sys
import os.path

from .submod import xyz

Upvotes: 9

Views: 629

Answers (1)

Ben
Ben

Reputation: 71495

I personally separate them. A __future__ import isn't just binding a name like other imports, it changes the meaning of the language. With things like from __future__ import division the module will likely run fine both with and without the import, but give different (wrong) results at places that have nothing telling me to go look at names imported if I want to know more about where they come from. __future__ imports should stand out as much as possible.

Also, I generally sort imports within a group alphabetically (no particularly good reason for doing that; I just find it has some very small benefits to diffs and merging branches), and __future__ imports have to be first, so I put them in their own group.

Upvotes: 12

Related Questions