import_stmt |
::= | "import" module ["as" name]
( "," module ["as" name] )* |
| "from" module "import" identifier
["as" name] | ||
( "," identifier ["as" name] )* | ||
| "from" module "import" "*" | ||
module |
::= | (identifier ".")* identifier |
Import statements are executed in two steps: (1) find a module, and initialize it if necessary; (2) define a name or names in the local namespace (of the scope where the import statement occurs). The first form (without from) repeats these steps for each identifier in the list. The form with from performs step (1) once, and then performs step (2) repeatedly.
import语句分两步执行:(1) 找到模块,如果需要则进行初始化;(2) 在(import语句所发生的范围内)局部名字空间中定义一个或者多个名字。(import语句的)第一形式(不带import那个)对列表中的每个标示符重复这些步骤。带import的形式只执行步骤(1)一次,然后重复地执行步骤(2)。
In this context, to ``initialize'' a built-in or extension module means to call an initialization function that the module must provide for the purpose (in the reference implementation, the function's name is obtained by prepending string ``init'' to the module's name); to ``initialize'' a Python-coded module means to execute the module's body.
The system maintains a table of modules that have been or are being
initialized,
indexed by module name. This table is
accessible as sys.modules
. When a module name is found in
this table, step (1) is finished. If not, a search for a module
definition is started. When a module is found, it is loaded. Details
of the module searching and loading process are implementation and
platform specific. It generally involves searching for a ``built-in''
module with the given name and then searching a list of locations
given as sys.path
.
系统维护一个已初始化的,由其名字索引的模块表。这个表可经由sys.modules访问。如果一个模块的名字可于该表找到,(该模块的)步骤(1)就已经结束了。如果没有,就开始搜索该模块的定义。一旦找到模块,它就会被载入(系统)。模块搜索和加载的详细过程取决于特定的实现和平台。一般地它涉及搜索给定名字的内置模块然后搜索由sys.path给出的位置列表。
If a built-in module is found, its built-in initialization code is executed and step (1) is finished. If no matching file is found, ImportError is raised. If a file is found, it is parsed, yielding an executable code block. If a syntax error occurs, SyntaxError is raised. Otherwise, an empty module of the given name is created and inserted in the module table, and then the code block is executed in the context of this module. Exceptions during this execution terminate step (1).
如果找到内置模块,就执行其内置的预置代码,然后步骤(1)就完成了。如果找不到匹配的文件,就引发ImportError异常。如果文件找到,它就会被分析,导致一段可执行代码块。如果有语法错误,就引发SyntaxError异常。否则,就用给定的名字创建一个空模块并插入模块表中,然后在这个模块的环境中运行所得代码块。在这一运行过程中引发的异常将中止步骤(1)。
When step (1) finishes without raising an exception, step (2) can begin.
如果步骤(1)无任何异常地结束了,步骤(2)就可以开始了
The first form of import statement binds the module name in the local namespace to the module object, and then goes on to import the next identifier, if any. If the module name is followed by as, the name following as is used as the local name for the module.
import语句的第一形式于当前局部名字空间中将模块名字约束到该模块对象上去,然后继续导入下个标识符,如果还有的话。如果模块名字后面带as,as后面跟着的名字就被用作该模块的局部名。为避免混淆,你不能把带点的模块名字导入为另外一个不同的名字。所以import module as m是合法的,但是import module.submod as s就不是。后者应写成from module import submod as s。参看下面所述。
The from form does not bind the module name: it goes through the list of identifiers, looks each one of them up in the module found in step (1), and binds the name in the local namespace to the object thus found. As with the first form of import, an alternate local name can be supplied by specifying "as localname". If a name is not found, ImportError is raised. If the list of identifiers is replaced by a star ("*"), all public names defined in the module are bound in the local namespace of the import statement..
带from的导入形式不约束模块的名字:它遍历其标识符列表,在由步骤(1)找到的模块中查找其中的每一个(所对应的对象),然后把局部空间中的名字约束到找到的对象上去。类似import的第一式,通过指定"as局部名"的形式,可以给出一个替代名字。如果某个名字找不到,就会引发ImportError异常。如果标识符列表用一个星号("*")取代,所有定义于该模块中的公共名字都在import语句所在的局部名字空间中被约束。
The public names defined by a module are determined by checking
the module's namespace for a variable named __all__
; if
defined, it must be a sequence of strings which are names defined or
imported by that module. The names given in __all__
are all
considered public and are required to exist. If __all__
is not
defined, the set of public names includes all names found in the
module's namespace which do not begin with an underscore character
("_"). __all__
should contain the entire public API.
It is intended to avoid accidentally exporting items that are not part
of the API (such as library modules which were imported and used within
the module).
一个模块所定义的“公共名字”通过检查该模块的名字空间中的名为__all__的变量决定。如果(该变量)有定义,它必须是一个字符串的有序序列,这些字符串是由该模块定义或者导入的名字。在__all__中给出的名字都被认为是公共的且要求其存在。如果__all__没有定义,(该模块的)公共名字的集合就包含所有在该模块的名字空间中找到的,不以下划线("_")起首的所有名字。
The from form with "*" may only occur in a module scope. If the wild card form of import -- "import *" -- is used in a function and the function contains or is a nested block with free variables, the compiler will raise a SyntaxError.
带*的from形式只能在模块的范围中发生。
Hierarchical module names:when the module names contains one or more dots, the module search
path is carried out differently. The sequence of identifiers up to
the last dot is used to find a ``package''; the final
identifier is then searched inside the package. A package is
generally a subdirectory of a directory on sys.path
that has a
file __init__.py.[XXX Can't be bothered to spell this out right now; see the URL
http://www.python.org/doc/essays/packages.html for more details, also
about how the module search works from inside a package.]
有层次的模块名字:当模块名字包含一个或多个小数点时,模块的搜索路径执行起来有所不同。(从头一直)到最后一个小数点的标识符序列被用来找到一个“包”;然后在该包中搜索最末的那个标识符。包一般而言是在sys.path中的目录的一个有文件__init__.py的子目录。【XXX 现在在这里不能把这些都详细地写出来,更多的细节,以及关于包内模块搜索如何工作的详情,请见URLhttp://www.python.org/doc/essays/packages.html 】
The built-in function __import__() is provided to support applications that determine which modules need to be loaded dynamically; refer to Built-in Functions in the Python Library Reference for additional information.
提供了内置函数__import__()使得应用程序可动态地决定需要加载哪些模块,更多信息请参考Python 库参考 中的内建函数 。