What's the correct way to sort Python `import x` and `from x import y` statements?
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Forest of Spells Looping
--
Chapters
00:00 Question
01:43 Accepted answer (Score 175)
03:19 Answer 2 (Score 74)
03:52 Answer 3 (Score 11)
04:54 Answer 4 (Score 8)
05:28 Thank you
--
Full question
https://stackoverflow.com/questions/2076...
Question links:
[python style guide]: http://www.python.org/dev/peps/pep-0008/...
[BDFL]: http://en.wikipedia.org/wiki/Guido_van_R...
Accepted answer links:
https://code.google.com/p/soc/wiki/Pytho...
https://github.com/reddit/reddit/wiki/Py...
http://docs.openstack.org/developer/hack.../
http://developer.plone.org/reference_man...
[isort utility]: https://pypi.python.org/pypi/isort/
Answer 2 links:
[WikiLeaks Vault 7 leak]: https://wikileaks.org/ciav7p1/cms/page_2...
Answer 3 links:
[Google recommend in this page]: https://google.github.io/styleguide/pygu...
Answer 4 links:
[Google code style guide]: http://google.github.io/styleguide/pygui...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #codingstyle #pythonimport #pep8
#avk47
ACCEPTED ANSWER
Score 195
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:
- https://code.google.com/p/soc/wiki/PythonStyleGuide
- https://github.com/reddit/reddit/wiki/PythonImportGuidelines
- http://docs.openstack.org/developer/hacking/
- http://developer.plone.org/reference_manuals/external/plone.api/contribute/conventions.html#grouping-and-sorting
PS: the isort utility automatically sorts your imports.
ANSWER 2
Score 83
According to the CIA's internal coding conventions (part of the WikiLeaks Vault 7 leak), python imports should be grouped into three groups:
- Standard library imports
- Third-party imports
- 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
ANSWER 3
Score 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:
- standard library imports
- related third party imports
- 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...
ANSWER 4
Score 7
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.