python里import turtle_[转] Python的import初探

先看一组示例:

>>>path

Traceback (most recent call last):

File"", line 1, in NameError: name'path' is notdefined>>>sys.path

Traceback (most recent call last):

File"", line 1, in NameError: name'sys' is notdefined>>> importsys>>>path

Traceback (most recent call last):

File"", line 1, in NameError: name'path' is notdefined>>>sys.path

['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk','/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload','/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages','/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/python2.7/dist-packages/gst-0.10','/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7']>>> from sys importpath>>>path

['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk','/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload','/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages','/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/python2.7/dist-packages/gst-0.10','/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7']

这段代码中,我们尝试使用sys包中的path变量得到python默认查找模块的路径信息,只有在import sys之后,python解释器才能正确的找到该变量.

我们通过一个python内部函数dir()来看看python解释器如何找到名字的. dir()函数是python内建函数,用于查看指定作用域下可用的名字.

若没有传参数,则打印当前作用域下的可用名字.

>>>help(dir)

Help on built-in function dir in module __builtin__:

dir(...)

dir([object])->list of strings

If called without an argument,return the names inthe current scope.

Else,returnan alphabetized list of names comprising (some of) the attributes

of the given object,and of attributes reachable fromit.

If the object supplies a method named__dir__, it will be used; otherwise

the default dir() logicis used andreturns:for a module object: the module's attributes. for a class object: its attributes, and recursively the attributes of its bases. for any other object: its attributes, its class's attributes, andrecursively the attributes of itsclass's base classes. >>> dir() ['__builtins__','__doc__','__name__','__package__'] >>> import sys >>> dir() ['__builtins__','__doc__','__name__','__package__','sys'] >>> dir(sys) [ ...,'modules','path', ... ,'version','version_info','warnoptions'] >>> from sys import path >>> dir() ['__builtins__','__doc__','__name__','__package__','path','sys']

执行import语句后,python解释器会将sys模块的名字添加到当前作用域中,这样就能直接通过sys.path得到python的搜索路径了.

注意到,我们还用了from sys import path语句.通过这条语句path就被直接提升到当前作用域中,这样path这个名字就能被直接使用了.

之所以有from,是为了更加精确的让某个模块中的某个名字在当前作用域可见.通过这种机制,程序员可以精确控制当前作用域的名字,防止作用域被不必要的名字污染.

另外,这种机制也避免了使用”.”来进行子成员的引用,减小程序员的输入.

这里需要提一句,虽然python提供了from XXX import *支持,能将XXX模块中的所有名字都提升到当前作用域中,但是要小心使用,因为程序员不能精确的知道到底import了哪些名字.

再看一组示例:

>>>dir()

['__builtins__', '__doc__', '__name__', '__package__']>>> importsys>>>dir()

['__builtins__', '__doc__', '__name__', '__package__', 'sys']>>> importsys as SYS>>>dir()

['SYS', '__builtins__', '__doc__', '__name__', '__package__', 'sys']>>>SYS.path

['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk','/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload','/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages','/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/python2.7/dist-packages/gst-0.10','/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7']>>>sys.path

['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk','/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload','/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages','/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/python2.7/dist-packages/gst-0.10','/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7']>>> del(sys)>>>SYS.path

['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk','/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload','/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages','/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/python2.7/dist-packages/gst-0.10','/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7']>>>sys.path

Traceback (most recent call last):

File"", line 1, in NameError: name'sys' is not defined

上面的例子展示了两个功能:

import XXX as YYY: 这个可以对模块实施重命名操作.

del(): 用于删除当前空间中不再使用的名字.当空间中出现很多不再需要的名字时,可以利用该函数进行清理.