• python中import的相关知识总结


     

    1. import 实际上是python虚拟机把当前的globals()和locals()传进__builtins__.__import__内置函数了,所以实际上干活的是那个__import__函数!

    2. import对命名空间的影响

    1)如果是python的内置模块,例如os模块。这些模块是随着python虚拟机启动而加载进来的,但是并没有暴露出来。我们可以通过dir()命令查看当前命名空间

    Python代码
    1. >>> dir()
    2. ['__builtins__', '__doc__', '__name__']
     
    1. >>> dir()
    2. ['__builtins__', '__doc__', '__name__']
    >>> dir()
    ['__builtins__', '__doc__', '__name__']
    

    可以看到,并没有看到像os,sys等模块。

    但是我们如果执行import sys后,那就有了。

    Python代码
    1. >>> import sys
    2. >>> dir()
    3. ['__builtins__', '__doc__', '__name__', 'sys']
     
    1. >>> import sys
    2. >>> dir()
    3. ['__builtins__', '__doc__', '__name__', 'sys']
    >>> import sys
    >>> dir()
    ['__builtins__', '__doc__', '__name__', 'sys']

    通过sys.modules这个字典(key: 模块名;value: 模块的路径),我们可以查看模块的信息。再通过id函数,我们可以知道两个sys模块是否为同一个模块,如下:

    Python代码
    1. >>> id(sys)
    2. 135708788
    3. >>> id(sys.modules['sys'])
    4. 135708788
     
    1. >>> id(sys)
    2. 135708788
    3. >>> id(sys.modules['sys'])
    4. 135708788
    >>> id(sys)
    135708788
    >>> id(sys.modules['sys'])
    135708788
    

    2)import只影响当前模块的命名空间

    例如a.py里面有一句import bb, 而bb.py里面有一句import os,那么可以这样查看

    Python代码
    1. >>> import a
    2. >>> dir()
    3. ['__builtins__', '__doc__', '__name__', 'a']
    4. >>> dir(a)
    5. ['__builtins__', '__doc__', '__file__', '__name__', 'bb']
    6. >>> dir(a.bb)
    7. ['__builtins__', '__doc__', '__file__', '__name__', 'os']
     
    1. >>> import a
    2. >>> dir()
    3. ['__builtins__', '__doc__', '__name__', 'a']
    4. >>> dir(a)
    5. ['__builtins__', '__doc__', '__file__', '__name__', 'bb']
    6. >>> dir(a.bb)
    7. ['__builtins__', '__doc__', '__file__', '__name__', 'os']
    >>> import a
    >>> dir()
    ['__builtins__', '__doc__', '__name__', 'a']
    >>> dir(a)
    ['__builtins__', '__doc__', '__file__', '__name__', 'bb']
    >>> dir(a.bb)
    ['__builtins__', '__doc__', '__file__', '__name__', 'os']

    3. import package

    首先合法的package必须含有一个__init__.py文件,package可以包含0个或多个module(py文件)。假设aa文件夹中有test.py文件和bb文件夹,bb文件夹里面有c.py。那么当import aa.bb.c的时候,究竟对命名空间产生什么影响呢?

    Python代码
    1. >>> import aa.bb.c
    2. >>> dir()
    3. ['__builtins__', '__doc__', '__name__', 'aa']
    4. >>> import sys
    5. >>> for k,v in sys.modules.items():
    6. ... print k,'\t',v
    7. ...
    8. <SPAN style="COLOR: #ff0000">aa <module 'aa' from 'aa/__init__.pyc'></SPAN>
    9. copy_reg <module 'copy_reg' from '/usr/local/lib/python2.5/copy_reg.pyc'>
    10. __main__ <module '__main__' (built-in)>
    11. site <module 'site' from '/usr/local/lib/python2.5/site.pyc'>
    12. __builtin__ <module '__builtin__' (built-in)>
    13. encodings <module 'encodings' from '/usr/local/lib/python2.5/encodings/__init__.pyc'>
    14. encodings.encodings None
    15. <SPAN style="COLOR: #ff0000">aa.bb.c <module 'aa.bb.c' from 'aa/bb/c.pyc'></SPAN>
    16. posixpath <module 'posixpath' from '/usr/local/lib/python2.5/posixpath.pyc'>
    17. errno <module 'errno' (built-in)>
    18. encodings.codecs None
    19. encodings.latin_1 <module 'encodings.latin_1' from '/usr/local/lib/python2.5/encodings/latin_1.pyc'>
    20. os.path <module 'posixpath' from '/usr/local/lib/python2.5/posixpath.pyc'>
    21. _codecs <module '_codecs' (built-in)>
    22. stat <module 'stat' from '/usr/local/lib/python2.5/stat.pyc'>
    23. zipimport <module 'zipimport' (built-in)>
    24. warnings <module 'warnings' from '/usr/local/lib/python2.5/warnings.pyc'>
    25. encodings.types None
    26. UserDict <module 'UserDict' from '/usr/local/lib/python2.5/UserDict.pyc'>
    27. sys <module 'sys' (built-in)>
    28. codecs <module 'codecs' from '/usr/local/lib/python2.5/codecs.pyc'>
    29. readline <module 'readline' from '/usr/local/lib/python2.5/lib-dynload/readline.so'>
    30. types <module 'types' from '/usr/local/lib/python2.5/types.pyc'>
    31. _types <module '_types' (built-in)>
    32. signal <module 'signal' (built-in)>
    33. linecache <module 'linecache' from '/usr/local/lib/python2.5/linecache.pyc'>
    34. posix <module 'posix' (built-in)>
    35. encodings.aliases <module 'encodings.aliases' from '/usr/local/lib/python2.5/encodings/aliases.pyc'>
    36. <SPAN style="COLOR: #ff0000">aa.bb <module 'aa.bb' from 'aa/bb/__init__.pyc'></SPAN>
    37. exceptions <module 'exceptions' (built-in)>
    38. os <module 'os' from '/usr/local/lib/python2.5/os.pyc'>
     
    1. >>> import aa.bb.c
    2. >>> dir()
    3. ['__builtins__', '__doc__', '__name__', 'aa']
    4. >>> import sys
    5. >>> for k,v in sys.modules.items():
    6. ... print k,'\t',v
    7. ...
    8. <SPAN style="COLOR: #ff0000">aa <module 'aa' from 'aa/__init__.pyc'></SPAN>
    9. copy_reg <module 'copy_reg' from '/usr/local/lib/python2.5/copy_reg.pyc'>
    10. __main__ <module '__main__' (built-in)>
    11. site <module 'site' from '/usr/local/lib/python2.5/site.pyc'>
    12. __builtin__ <module '__builtin__' (built-in)>
    13. encodings <module 'encodings' from '/usr/local/lib/python2.5/encodings/__init__.pyc'>
    14. encodings.encodings None
    15. <SPAN style="COLOR: #ff0000">aa.bb.c <module 'aa.bb.c' from 'aa/bb/c.pyc'></SPAN>
    16. posixpath <module 'posixpath' from '/usr/local/lib/python2.5/posixpath.pyc'>
    17. errno <module 'errno' (built-in)>
    18. encodings.codecs None
    19. encodings.latin_1 <module 'encodings.latin_1' from '/usr/local/lib/python2.5/encodings/latin_1.pyc'>
    20. os.path <module 'posixpath' from '/usr/local/lib/python2.5/posixpath.pyc'>
    21. _codecs <module '_codecs' (built-in)>
    22. stat <module 'stat' from '/usr/local/lib/python2.5/stat.pyc'>
    23. zipimport <module 'zipimport' (built-in)>
    24. warnings <module 'warnings' from '/usr/local/lib/python2.5/warnings.pyc'>
    25. encodings.types None
    26. UserDict <module 'UserDict' from '/usr/local/lib/python2.5/UserDict.pyc'>
    27. sys <module 'sys' (built-in)>
    28. codecs <module 'codecs' from '/usr/local/lib/python2.5/codecs.pyc'>
    29. readline <module 'readline' from '/usr/local/lib/python2.5/lib-dynload/readline.so'>
    30. types <module 'types' from '/usr/local/lib/python2.5/types.pyc'>
    31. _types <module '_types' (built-in)>
    32. signal <module 'signal' (built-in)>
    33. linecache <module 'linecache' from '/usr/local/lib/python2.5/linecache.pyc'>
    34. posix <module 'posix' (built-in)>
    35. encodings.aliases <module 'encodings.aliases' from '/usr/local/lib/python2.5/encodings/aliases.pyc'>
    36. <SPAN style="COLOR: #ff0000">aa.bb <module 'aa.bb' from 'aa/bb/__init__.pyc'></SPAN>
    37. exceptions <module 'exceptions' (built-in)>
    38. os <module 'os' from '/usr/local/lib/python2.5/os.pyc'>
    >>> import aa.bb.c
    >>> dir()
    ['__builtins__', '__doc__', '__name__', 'aa']
    >>> import sys 
    >>> for k,v in sys.modules.items():
    ...     print k,'\t',v
    ... 
    aa 	<module 'aa' from 'aa/__init__.pyc'>
    
    
    
    copy_reg 	<module 'copy_reg' from '/usr/local/lib/python2.5/copy_reg.pyc'>
    __main__ 	<module '__main__' (built-in)>
    site 	<module 'site' from '/usr/local/lib/python2.5/site.pyc'>
    __builtin__ 	<module '__builtin__' (built-in)>
    encodings 	<module 'encodings' from '/usr/local/lib/python2.5/encodings/__init__.pyc'>
    encodings.encodings 	None
    aa.bb.c 	<module 'aa.bb.c' from 'aa/bb/c.pyc'>
    
    
    
    posixpath 	<module 'posixpath' from '/usr/local/lib/python2.5/posixpath.pyc'>
    errno 	<module 'errno' (built-in)>
    encodings.codecs 	None
    encodings.latin_1 	<module 'encodings.latin_1' from '/usr/local/lib/python2.5/encodings/latin_1.pyc'>
    os.path 	<module 'posixpath' from '/usr/local/lib/python2.5/posixpath.pyc'>
    _codecs 	<module '_codecs' (built-in)>
    stat 	<module 'stat' from '/usr/local/lib/python2.5/stat.pyc'>
    zipimport 	<module 'zipimport' (built-in)>
    warnings 	<module 'warnings' from '/usr/local/lib/python2.5/warnings.pyc'>
    encodings.types 	None
    UserDict 	<module 'UserDict' from '/usr/local/lib/python2.5/UserDict.pyc'>
    sys 	<module 'sys' (built-in)>
    codecs 	<module 'codecs' from '/usr/local/lib/python2.5/codecs.pyc'>
    readline 	<module 'readline' from '/usr/local/lib/python2.5/lib-dynload/readline.so'>
    types 	<module 'types' from '/usr/local/lib/python2.5/types.pyc'>
    _types 	<module '_types' (built-in)>
    signal 	<module 'signal' (built-in)>
    linecache 	<module 'linecache' from '/usr/local/lib/python2.5/linecache.pyc'>
    posix 	<module 'posix' (built-in)>
    encodings.aliases 	<module 'encodings.aliases' from '/usr/local/lib/python2.5/encodings/aliases.pyc'>
    aa.bb 	<module 'aa.bb' from 'aa/bb/__init__.pyc'>
    
    
    
    exceptions 	<module 'exceptions' (built-in)>
    os 	<module 'os' from '/usr/local/lib/python2.5/os.pyc'>

    可以看到只是一句import aa.bb.c,却把aa,aa.bb,aabb.c全部加入了当前命名空间。这种做法是为了防止重名的吧,因为如果不加前缀的话,其他包里面有重名的模块就不能识别了!原理是python现在当前命名空间查找符号‘aa’对应的object,然后再在它的命名空间查找符号'bb',然后在bb的属性(命名空间)里面寻找c,所以,需要把aa,bb都加载进来。不过这些都是只加载一次的,不信的话,大家可以试一下再import aa.bb.d这样的模块,同时预先在aa文件夹中的

    __init__.py文件中输入print ‘hello’,hello只在第一次import aa.bb.c的时候输出!

    4.from与import

    1)例如:from aa import bb

    Python代码
    1. >>> from aa import bb
    2. >>> dir()
    3. ['__builtins__', '__doc__', '__name__', 'bb']
    4. >>> import sys
    5. >>> sys.modules['bb']
    6. Traceback (most recent call last):
    7. File "<stdin>", line 1, in <module>
    8. KeyError: 'bb'
    9. >>> sys.modules['aa.bb']
    10. <module 'aa.bb' from 'aa/bb/__init__.pyc'>
    11. >>> bb
    12. <module 'aa.bb' from 'aa/bb/__init__.pyc'>
    13. >>> aa.bb
    14. Traceback (most recent call last):
    15. File "<stdin>", line 1, in <module>
    16. NameError: name 'aa' is not defined
     
    1. >>> from aa import bb
    2. >>> dir()
    3. ['__builtins__', '__doc__', '__name__', 'bb']
    4. >>> import sys
    5. >>> sys.modules['bb']
    6. Traceback (most recent call last):
    7. File "<stdin>", line 1, in <module>
    8. KeyError: 'bb'
    9. >>> sys.modules['aa.bb']
    10. <module 'aa.bb' from 'aa/bb/__init__.pyc'>
    11. >>> bb
    12. <module 'aa.bb' from 'aa/bb/__init__.pyc'>
    13. >>> aa.bb
    14. Traceback (most recent call last):
    15. File "<stdin>", line 1, in <module>
    16. NameError: name 'aa' is not defined
    >>> from aa import bb
    >>> dir()
    ['__builtins__', '__doc__', '__name__', 'bb']
    >>> import sys 
    >>> sys.modules['bb']
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    KeyError: 'bb'
    >>> sys.modules['aa.bb']
    <module 'aa.bb' from 'aa/bb/__init__.pyc'>
    >>> bb
    <module 'aa.bb' from 'aa/bb/__init__.pyc'>
    >>> aa.bb
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'aa' is not defined

    from import只是把import后面的名字引入了命名空间,让我们可以更方便的 使用而已,但是内部实际上还是带有模块名的。import 。。。as。。。也是同样的道理了。

    2)from 。。。import *

    这种形式的import是不推荐的,因为很容易污染命名空间。但是也可以了解一下。

    首先如果package中的__init__.py文件并没有声明__all__ 的值的话,from package import * 是没有用的。因为python源码中的import_from_all这个函数估计会去读__all__的值,然后再import。

    而这种形式的import对命名空间的影响又是怎样的呢?

    答案是和import aa.bb.c那种一样的。

  • 相关阅读:
    ER模型
    一道人人的笔试题
    关系代数运算
    推荐两个不错的CAD二次开发(.Net)手册
    CAD 致命错误
    CAD二次开发(.NET)之PaletteSet和Palette
    养生
    我看面向对象
    .NET中参数化查询数据库
    自定义按照index和key访问的List
  • 原文地址:https://www.cnblogs.com/blockcipher/p/3006751.html
Copyright © 2020-2023  润新知