对 Python `import x` 和 `from x import y` 语句进行排序的正确方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20762662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:09:46  来源:igfitidea点击:

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

pythoncoding-stylepython-importpep8

提问by ThiefMaster

The python style guidesuggests 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

进口应按以下顺序分组:

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

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
    
  • 首先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 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.

PEP8 没有提到这个的首选顺序,一些 IDE 可能只是做任何该功能的开发人员喜欢的“清理导入”功能。

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.

我正在寻找另一个 PEP 来澄清这一点或来自BDFL(或另一个 Python 核心开发人员)的相关评论/电子邮件请不要发表主观的答案,陈述您自己的喜好。

采纳答案by Abhishek

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

进口通常按字母顺序排序,并在 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

按字母顺序排序的模块更易于阅读和搜索。毕竟python是关于可读性的。也更容易验证某些东西被导入,并避免重复导入

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

PEP 8 中没有关于排序的可用内容。所以它完全取决于您选择使用什么。

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

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

References:

参考:

PS: the isort utilityautomatically sorts your imports.

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

回答by Maxime Lorant

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:

PEP 8 确实对此一无所知。在这一点上没有约定,这并不意味着 Python 社区需要绝对定义一个。一个选择对一个项目来说可能更好,但对另一个项目来说可能是最糟糕的......这是一个偏好问题,因为每个解决方案都有优点和缺点。但是如果你想遵循约定,你必须尊重你引用的主要顺序:

  1. standard library imports
  2. related third party imports
  3. local application/library specific imports
  1. 标准库导入
  2. 相关第三方进口
  3. 本地应用程序/库特定导入

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

例如,Google 在此页面建议导入应按字典顺序排序,在每个类别中(标准/第三方/您的)。但在 Facebook、雅虎等等,这可能是另一种惯例......

回答by gar

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

根据 CIA 的内部编码约定(维基解密 Vault 7 泄漏的一部分),python 导入应分为三组:

  1. Standard library imports
  2. Third-party imports
  3. Application-specific imports
  1. 标准库导入
  2. 第三方进口
  3. 特定于应用程序的导入

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

回答by Astitva Srivastava

All import xstatements should be sorted by the value of xand all from x import ystatements should be sorted by the value of xin alphabetical order and the sorted groups of from x import ystatements must follow the sorted group of import xstatements.

所有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

回答by dthor

I highly recommend reorder-python-imports. It follows the 2nd option of the accepted answerand also integrates into pre-commit, which is super helpful.

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