284

The python style guide suggests to group imports like this:

Imports should be grouped in the following order:

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

However, it does not mention anything how the two different ways of imports should be laid out:

from foo import bar
import foo

There are multiple ways to sort them (let's assume all those import belong to the same group):

  • first from..import, then import

    from g import gg
    from x import xx
    import abc
    import def
    import x
    
  • first import, then from..import

    import abc
    import def
    import x
    from g import gg
    from x import xx
    
  • alphabetic order by module name, ignoring the kind of import

    import abc
    import def
    from g import gg
    import x
    from xx import xx
    

PEP8 does not mention the preferred order for this and the "cleanup imports" features some IDEs have probably just do whatever the developer of that feature preferred.

I'm looking for another PEP clarifying this or a relevant comment/email from the BDFL (or another Python core developer). Please don't post subjective answers stating your own preference.

1

6 Answers 6

203

Imports are generally sorted alphabetically and described in various places besides PEP 8.

Alphabetically sorted modules are quicker to read and searchable. After all, Python is all about readability. Also, it is easier to verify that something is imported, and avoids duplicate imports.

There is nothing available in PEP 8 regarding sorting. So it's all about choosing what you use.

According to few references from reputable sites and repositories, also popularity, Alphabetical ordering is the way.

for e.g. like this:

import httplib
import logging
import random
import StringIO
import time
import unittest
from nova.api import openstack
from nova.auth import users
from nova.endpoint import cloud

OR

import a_standard
import b_standard

import a_third_party
import b_third_party

from a_soc import f
from a_soc import g
from b_soc import d

Reddit official repository also states that In general PEP-8 import ordering should be used. However, there are a few additions which are that for each imported group the order of imports should be:

import <package>.<module> style lines in alphabetical order
from <package>.<module> import <symbol> style in alphabetical order

References:

PS: the isort utility automatically sorts your imports.

Sign up to request clarification or add additional context in comments.

8 Comments

I don't see where you're answering the actual question…
@liori I submitted a reference/relevant links stating how to sort modules . As PEP 8 don't mention anything but many another references do suggest to use import methods like this.
Note that the question is about sorting import x and from y import z statements relative to each other. I see no answer to this question in your answer. You're basically restating part of the question that already explains the PEP8 way of grouping by import type. If an answer to this specific question is in some of the links, please quote the relevant parts.
What order should be used for private vs public imports? (import _tkinter vs import unittest)
I highly recommend reorder-python-imports. It follows the 2nd option of the accepted answer and also integrates into pre-commit, which is super helpful.
|
85

According to the CIA's internal coding conventions (part of the WikiLeaks Vault 7 leak), python imports should be grouped into three groups:

  1. Standard library imports
  2. Third-party imports
  3. Application-specific imports

Imports should be ordered lexicographically within these groups, ignoring case:

import foo
from foo import bar
from foo.bar import baz
from foo.bar import Quux
from Foob import ar

5 Comments

But it does not follow from the example if from x import y should be before or after from y import x - is it the name of the module or the actual import which determines the order?
where would this import go? from foo.abc.def import ghi
ok, but within each group, should the "import"s come before the "from"s ? For example is this order correct ? "import json", "from io import StringIO" and "import pandas as pd" ?
12

I feel like the accepted answer is a bit too verbose. Here is TLDR:

Within each grouping, imports should be sorted lexicographically, ignoring case, according to each module’s full package path

Google code style guide

So, the third option is correct:

import abc
import def
from g import yy  # changed gg->yy for illustrative purposes
import x
from xx import xx

1 Comment

Worth noting this isn't an official rule from the maintainers of python, this is just one particular style guide
11

The PEP 8 says nothing about it indeed. There's no convention for this point, and it doesn't mean the Python community need to define one absolutely. A choice can be better for a project but the worst for another... It's a question of preferences for this, since each solutions has pro and cons. But if you want to follow conventions, you have to respect the principal order you quoted:

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

For example, Google recommend in this page that import should be sorted lexicographically, in each categories (standard/third parties/yours). But at Facebook, Yahoo and whatever, it's maybe another convention...

1 Comment

To answer the question more directly, the Google style guides recommend sorting by the path, which is the third option from the question: "alphabetic order by module name, ignoring the kind of import"
3

All import x statements should be sorted by the value of x and all from x import y statements should be sorted by the value of x in alphabetical order and the sorted groups of from x import y statements must follow the sorted group of import x statements.

import abc
import def
import x
from g import gg
from x import xx
from z import a

2 Comments

I don’t think you can import def. It would raise a SyntaxError…
@DeepThought42 def is not a module, rather it is a keyword. I was using a random module name to import. The idea is to show the sequence based on alphabetical sorting.
-1

I use Sort lines VScode extension, the sorted result is:

(Sort lines(ascending, case sensitive)):

from g import gg
from x import xx
import abc
import def
import x

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.