What's the correct way to sort Python `import x` and `from x import y` statements?

后端 未结 6 1139
半阙折子戏
半阙折子戏 2020-12-04 05:32

The python style guide suggests to group imports like this:

Imports should be grouped in the following order:

  1. standard library imp
相关标签:
6条回答
  • 2020-12-04 06:04

    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
    
    0 讨论(0)
  • 2020-12-04 06:05

    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.

    0 讨论(0)
  • 2020-12-04 06:09

    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...

    0 讨论(0)
  • 2020-12-04 06:18

    Imports are generally sorted alphabetically and described in various places beside 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 duplicated imports

    There is nothing available in PEP 8 regarding sorting.So its all about choice what you use.

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

    for eg 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 is

    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.

    0 讨论(0)
  • 2020-12-04 06:20

    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
    
    0 讨论(0)
  • 2020-12-04 06:23

    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
    
    0 讨论(0)
提交回复
热议问题