1. import 实际上是python虚拟机把当前的globals()和locals()传进__builtins__.__import__内置函数了,所以实际上干活的是那个__import__函数!
2. import对命名空间的影响
1)如果是python的内置模块,例如os模块。这些模块是随着python虚拟机启动而加载进来的,但是并没有暴露出来。我们可以通过dir()命令查看当前命名空间
- >>> dir()
- ['__builtins__', '__doc__', '__name__']
- >>> dir()
- ['__builtins__', '__doc__', '__name__']
>>> dir() ['__builtins__', '__doc__', '__name__']
可以看到,并没有看到像os,sys等模块。
但是我们如果执行import sys后,那就有了。
- >>> import sys
- >>> dir()
- ['__builtins__', '__doc__', '__name__', 'sys']
- >>> import sys
- >>> dir()
- ['__builtins__', '__doc__', '__name__', 'sys']
>>> import sys >>> dir() ['__builtins__', '__doc__', '__name__', 'sys']
通过sys.modules这个字典(key: 模块名;value: 模块的路径),我们可以查看模块的信息。再通过id函数,我们可以知道两个sys模块是否为同一个模块,如下:
- >>> id(sys)
- 135708788
- >>> id(sys.modules['sys'])
- 135708788
- >>> id(sys)
- 135708788
- >>> id(sys.modules['sys'])
- 135708788
>>> id(sys) 135708788 >>> id(sys.modules['sys']) 135708788
2)import只影响当前模块的命名空间
例如a.py里面有一句import bb, 而bb.py里面有一句import os,那么可以这样查看
- >>> import a
- >>> dir()
- ['__builtins__', '__doc__', '__name__', 'a']
- >>> dir(a)
- ['__builtins__', '__doc__', '__file__', '__name__', 'bb']
- >>> dir(a.bb)
- ['__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']
>>> 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的时候,究竟对命名空间产生什么影响呢?
- >>> import aa.bb.c
- >>> dir()
- ['__builtins__', '__doc__', '__name__', 'aa']
- >>> import sys
- >>> for k,v in sys.modules.items():
- ... print k,'\t',v
- ...
- <SPAN style="COLOR: #ff0000">aa <module 'aa' from 'aa/__init__.pyc'></SPAN>
- 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
- <SPAN style="COLOR: #ff0000">aa.bb.c <module 'aa.bb.c' from 'aa/bb/c.pyc'></SPAN>
- 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'>
- <SPAN style="COLOR: #ff0000">aa.bb <module 'aa.bb' from 'aa/bb/__init__.pyc'></SPAN>
- exceptions <module 'exceptions' (built-in)>
- 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
- ...
- <SPAN style="COLOR: #ff0000">aa <module 'aa' from 'aa/__init__.pyc'></SPAN>
- 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
- <SPAN style="COLOR: #ff0000">aa.bb.c <module 'aa.bb.c' from 'aa/bb/c.pyc'></SPAN>
- 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'>
- <SPAN style="COLOR: #ff0000">aa.bb <module 'aa.bb' from 'aa/bb/__init__.pyc'></SPAN>
- exceptions <module 'exceptions' (built-in)>
- 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
- >>> 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 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 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那种一样的。