什么是排序Python`import x`和`from x import y`语句的正确方法? [英] What's the correct way to sort Python `import x` and `from x import y` statements?

查看:383
本文介绍了什么是排序Python`import x`和`from x import y`语句的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python风格指南建议按以下方式对导入进行分组:


导入应按以下顺序分组:






  1. / blockquote>

    但是,它没有提到应该如何布局两种不同的导入方式:

      from foo import bar 
    import foo

    (我们假设所有导入属于同一组):




    • 第一 .import ,然后 import

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


    • 首先 import ,然后 from..import

        import abc 
      import def
      import x
      从g import gg
      从x import xx


    • 按模块名称按字母排序,忽略导入类型

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



    $ b $ p

    PEP8没有提及此优先顺序和导入功能,一些IDE可能只是做任何该功能的开发人员喜欢的。



    我正在寻找另一个PEP澄清这个或相关的评论/电子邮件来自 BDFL (或另一个Python核心开发人员)。

    解决方案

    导入通常按字母顺序排序,在PEP 8旁边的各个地方描述。



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



    PEP 8中没有什么可用的关于排序。所有关于选择什么



    根据信誉良好的网站和知识库的少数参考资料也很受欢迎,按字母顺序排列。



    例如像这样:

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

      import a_standard 
    import b_standard

    import a_third_party
    import b_third_party

    从a_soc导入f
    从a_soc导入g
    从b_soc导入d

    Reddit官方库还声明,一般来说,应使用PEP-8导入排序。但是,对于每个导入组,有一些添加项是

     。导入顺序应该是:
    import< package>。< module>从< package>按字母顺序从
    中选择样式行。< module> import< symbol>风格按字母顺序

    参考文献:





    PS也有python包也按字母顺序导入模块
    https:// pypi .python.org/pypi/ isort / 2.6.2


    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.

    解决方案

    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 there is python package too for importing modules alphabetically https://pypi.python.org/pypi/isort/2.6.2

    这篇关于什么是排序Python`import x`和`from x import y`语句的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆