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

前端 未结 19 2119
一向
一向 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:19

    Even though many people already explained about import vs import from, I want to try to explain a bit more about what happens under the hood, and where all the places it changes are.


    import foo:

    Imports foo, and creates a reference to that module in the current namespace. Then you need to define completed module path to access a particular attribute or method from inside the module.

    E.g. foo.bar but not bar

    from foo import bar:

    Imports foo, and creates references to all the members listed (bar). Does not set the variable foo.

    E.g. bar but not baz or foo.baz

    from foo import *:

    Imports foo, and creates references to all public objects defined by that module in the current namespace (everything listed in __all__ if __all__ exists, otherwise everything that doesn't start with _). Does not set the variable foo.

    E.g. bar and baz but not _qux or foo._qux.


    Now let’s see when we do import X.Y:

    >>> import sys
    >>> import os.path
    

    Check sys.modules with name os and os.path:

    >>> sys.modules['os']
    
    >>> sys.modules['os.path']
    
    

    Check globals() and locals() namespace dicts with os and os.path:

    >>> globals()['os']
    
    >>> locals()['os']
    
    >>> globals()['os.path']
    Traceback (most recent call last):
      File "", line 1, in 
    KeyError: 'os.path'
    >>>
    

    From the above example we found that only os is inserted in the local and global namespace. So, we should be able to use:

    >>> os
    
    >>> os.path
    
    >>>
    

    But not path.

    >>> path
    Traceback (most recent call last):
      File "", line 1, in 
    NameError: name 'path' is not defined
    >>>
    

    Once you delete the os from locals() namespace, you won't be able to access os as well as os.path even though they exist in sys.modules:

    >>> del locals()['os']
    >>> os
    Traceback (most recent call last):
      File "", line 1, in 
    NameError: name 'os' is not defined
    >>> os.path
    Traceback (most recent call last):
      File "", line 1, in 
    NameError: name 'os' is not defined
    >>>
    

    Now let's talk about import from:

    from:

    >>> import sys
    >>> from os import path
    

    Check sys.modules with os and os.path:

    >>> sys.modules['os']
    
    >>> sys.modules['os.path']
    
    

    We found that in sys.modules we found as same as we did before by using import name

    OK, let's check how it looks like in locals() and globals() namespace dicts:

    >>> globals()['path']
    
    >>> locals()['path']
    
    >>> globals()['os']
    Traceback (most recent call last):
      File "", line 1, in 
    KeyError: 'os'
    >>>
    

    You can access by using name path not by os.path:

    >>> path
    
    >>> os.path
    Traceback (most recent call last):
      File "", line 1, in 
    NameError: name 'os' is not defined
    >>>
    

    Let's delete 'path' from locals():

    >>> del locals()['path']
    >>> path
    Traceback (most recent call last):
      File "", line 1, in 
    NameError: name 'path' is not defined
    >>>
    

    One final example using an alias:

    >>> from os import path as HELL_BOY
    >>> locals()['HELL_BOY']
    
    >>> globals()['HELL_BOY']
    
    >>>
    

    And no path defined:

    >>> globals()['path']
    Traceback (most recent call last):
     File "", line 1, in 
    KeyError: 'path'
    >>>
    

提交回复
热议问题