Use 'import module' or 'from module import'?

前端 未结 19 2465
一向
一向 2020-11-21 07:47

I\'ve tried to find a comprehensive guide on whether it is best to use import module or from module import. I\'ve just started with Python and I\'m

19条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-21 08:04

    I would like to add to this, there are somethings to consider during the import calls:

    I have the following structure:

    mod/
        __init__.py
        main.py
        a.py
        b.py
        c.py
        d.py
    

    main.py:

    import mod.a
    import mod.b as b
    from mod import c
    import d
    

    dis.dis shows the difference:

      1           0 LOAD_CONST               0 (-1)
                  3 LOAD_CONST               1 (None)
                  6 IMPORT_NAME              0 (mod.a)
                  9 STORE_NAME               1 (mod)
    
      2          12 LOAD_CONST               0 (-1)
                 15 LOAD_CONST               1 (None)
                 18 IMPORT_NAME              2 (b)
                 21 STORE_NAME               2 (b)
    
      3          24 LOAD_CONST               0 (-1)
                 27 LOAD_CONST               2 (('c',))
                 30 IMPORT_NAME              1 (mod)
                 33 IMPORT_FROM              3 (c)
                 36 STORE_NAME               3 (c)
                 39 POP_TOP
    
      4          40 LOAD_CONST               0 (-1)
                 43 LOAD_CONST               1 (None)
                 46 IMPORT_NAME              4 (mod.d)
                 49 LOAD_ATTR                5 (d)
                 52 STORE_NAME               5 (d)
                 55 LOAD_CONST               1 (None)
    

    In the end they look the same (STORE_NAME is result in each example), but this is worth noting if you need to consider the following four circular imports:

    example1

    foo/
       __init__.py
       a.py
       b.py
    
    a.py:
    import foo.b 
    
    b.py:
    import foo.a
    
    >>> import foo.a
    >>>
    

    This works

    example2

    bar/
       __init__.py
       a.py
       b.py
    
    a.py:
    import bar.b as b
    
    b.py:
    import bar.a as a
    
    >>> import bar.a
    Traceback (most recent call last):
      File "", line 1, in 
      File "bar\a.py", line 1, in 
        import bar.b as b
      File "bar\b.py", line 1, in 
        import bar.a as a
    AttributeError: 'module' object has no attribute 'a'
    

    No dice

    example3

    baz/
       __init__.py
       a.py
       b.py
    
    a.py:
    from baz import b
    
    b.py:
    from baz import a
    
    >>> import baz.a
    Traceback (most recent call last):
      File "", line 1, in 
      File "baz\a.py", line 1, in 
        from baz import b
      File "baz\b.py", line 1, in 
        from baz import a
    ImportError: cannot import name a
    

    Similar issue... but clearly from x import y is not the same as import import x.y as y

    example4

    qux/
       __init__.py
       a.py
       b.py
    
    a.py:
    import b 
    
    b.py:
    import a
    
    >>> import qux.a
    >>>
    

    This one also works

提交回复
热议问题