问题:排序Python`import x`和`from x import y`语句的正确方法是什么?

Python风格指南建议对进口组这样的:

导入应按以下顺序分组:

  1. 标准库导入
  2. 相关第三方进口
  3. 本地应用程序/特定于库的导入

但是,它没有提及应如何布置两种不同的进口方式的内容:

from foo import bar
import foo

对它们进行排序有多种方法(假设所有这些导入都属于同一组):

  • 首先from..import,然后import

    from g import gg
    from x import xx
    import abc
    import def
    import x
    
  • 首先import,然后from..import

    import abc
    import def
    import x
    from g import gg
    from x import xx
    
  • 按模块名称的字母顺序,忽略导入的类型

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

PEP8没有提到此命令的首选顺序,某些IDE的“清除导入”功能可能只是该工具的开发人员所做的任何事情。

我正在寻找另一个PEP来澄清这一点,或从BDFL(或另一个Python核心开发人员)那里获得相关的评论/电子邮件请不要发布主观答案来说明您的个人偏好。

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.


回答 0

进口商品通常按字母顺序排序,并在PEP 8的不同位置进行描述。

按字母顺序排序的模块更易于阅读和搜索。毕竟python都是关于可读性的。另外,更容易验证是否导入了某些内容,并避免了重复的导入

PEP 8中没有关于排序的任何信息,因此所有关于选择所用内容的信息都是如此。

根据知名站点和存储库中也很少使用的参考文献,按字母顺序排序是这种方式。

例如:

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

要么

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官方存储库还指出,通常应使用PEP-8导入顺序。但是有一些补充是

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

参考文献:

PS:isort实用程序会自动对您的导入进行排序。

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:

PS: the isort utility automatically sorts your imports.


回答 1

根据CIA的内部编码约定(WikiLeaks Vault 7泄漏的一部分),python导入应分为三类:

  1. 标准库导入
  2. 第三方进口
  3. 特定于应用程序的导入

在这些组中,应按字典顺序对导入进行排序,而忽略大小写:

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

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

回答 2

PEP 8对此一无所获。关于这一点,没有约定,这并不意味着Python社区需要绝对定义一个。对于一个项目而言,选择可能会更好,而对于另一个项目而言,则是最糟糕的……这是一个偏好设置的问题,因为每种解决方案都有其优缺点。但是,如果要遵循约定,则必须遵守引用的主要顺序:

  1. 标准库导入
  2. 相关第三方进口
  3. 本地应用程序/特定于库的导入

例如,Google在此页面建议导入应按字典类别在每个类别(标准/第三方/您的)中进行排序。但是在Facebook,Yahoo和其他地方,这可能是另一种惯例…

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…


回答 3

我强烈建议reorder-python-imports。它遵循已接受答案的第二个选项,并且还集成到pre-commit中,这非常有帮助。

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.


回答 4

所有import x语句应按的值排序x,所有from x import y语句应按的值x按字母顺序排序,并且已排序的from x import y语句组必须遵循已排序的import x语句组。

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

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

回答 5

我觉得已接受的答案有点太冗长。这是TLDR:

在每个分组中,应按照每个模块的完整包装路径,按字典顺序对导入进行排序,而忽略大小写

Google代码样式指南

因此,第三个选项是正确的:

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

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

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。