Friday, July 13, 2012

import

import可以有一下几种形式:
  • import A (推荐都使用这种方式)
        将A导入到当前的namespace
        变种可以时 import A.B.C.D 如果是这种情况 ABC必须是Package 而D只能是一个模块 或者时一个Pakcage

  • from A import B
        将B导入到当前的namespace 当改变B时 对B作出的修改不会影响到A.B

  • X = __import__('X') (运行时才知道X的名称)

  • from A import *
        将A中所有的public name引入到当前的namespace public name在这里指的是name明称不以_(下划线)开头的那些name


import vs loading

一个模块不管被imoprt多少次 它只会被load一次 如果有执行代码在这个模块里面 那么只有第一次被加载的时候会运行 之后import则不会去运行未包装的执行代码



What does python do to import a Module?
1, check sys.modules to see if module is already imported, if that's the case, python will use the existing module object as is.
2, otherwise, create a new , empty module object
3, insert that module in sys.modules
4, Load the module code object
5, Execute the module code object in the new module's namespace. All variables assigned by the code will be available via the module object.


Importing * From a Package

The only solution is for the package author to provide an explicit index of the package. The import statement uses the following convention: if a package’s __init__.py code defines a list named __all__, it is taken to be the list of module names that should be imported when from package import * is encountered. It is up to the package author to keep this list up-to-date when a new version of the package is released. Package authors may also decide not to support it, if they don’t see a use for importing * from their package.


If __all__ is not defined, the statement from sound.effects import * does not import all submodules from the package sound.effects into the current namespace; it only ensures that the package sound.effects has been imported (possibly running any initialization code in __init__.py) and then imports whatever names are defined in the package. This includes any names defined (and submodules explicitly loaded) by __init__.py. It also includes any submodules of the package that were explicitly loaded by previous import statements. Consider this code:
import sound.effects.echo
import sound.effects.surround
from sound.effects import *
In this example, the echo and surround modules are imported in the current namespace because they are defined in the sound.effects package when the from...import statement is executed. (This also works when __all__ is defined.)

3 comments:

  1. 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.

    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.

    ReplyDelete
  2. 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.

    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).

    When step (1) finishes without raising an exception, step (2) can begin.

    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.

    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..

    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).

    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.

    ReplyDelete
  3. 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.]

    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.

    http://docs.python.org/release/2.5/ref/import.html

    ReplyDelete