| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  | \section{Built-in Module \sectcode{imp}} | 
					
						
							| 
									
										
										
										
											1995-01-10 10:51:08 +00:00
										 |  |  | \bimodindex{imp} | 
					
						
							|  |  |  | \index{import} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-03-07 10:14:09 +00:00
										 |  |  | This module provides an interface to the mechanisms used to implement | 
					
						
							| 
									
										
										
										
											1995-01-10 10:51:08 +00:00
										 |  |  | the \code{import} statement.  It defines the following constants and | 
					
						
							|  |  |  | functions: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \renewcommand{\indexsubitem}{(in module struct)} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{get_magic}{} | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  | Return the magic string value used to recognize byte-compiled code | 
					
						
							| 
									
										
										
										
											1995-01-10 10:51:08 +00:00
										 |  |  | files (``\code{.pyc} files''). | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{get_suffixes}{} | 
					
						
							|  |  |  | Return a list of triples, each describing a particular type of file. | 
					
						
							|  |  |  | Each triple has the form \code{(\var{suffix}, \var{mode}, | 
					
						
							|  |  |  | \var{type})}, where \var{suffix} is a string to be appended to the | 
					
						
							|  |  |  | module name to form the filename to search for, \var{mode} is the mode | 
					
						
							|  |  |  | string to pass to the built-in \code{open} function to open the file | 
					
						
							|  |  |  | (this can be \code{'r'} for text files or \code{'rb'} for binary | 
					
						
							|  |  |  | files), and \var{type} is the file type, which has one of the values | 
					
						
							|  |  |  | \code{PY_SOURCE}, \code{PY_COMPILED} or \code{C_EXTENSION}, defined | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  | below.  (System-dependent values may also be returned.) | 
					
						
							| 
									
										
										
										
											1995-01-10 10:51:08 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{find_module}{name\, \optional{path}} | 
					
						
							|  |  |  | Try to find the module \var{name} on the search path \var{path}.  The | 
					
						
							|  |  |  | default \var{path} is \code{sys.path}.  The return value is a triple | 
					
						
							|  |  |  | \code{(\var{file}, \var{pathname}, \var{description})} where | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  | \var{file} is an open file object positioned at the beginning, | 
					
						
							|  |  |  | \var{pathname} is the pathname of the | 
					
						
							| 
									
										
										
										
											1995-01-10 10:51:08 +00:00
										 |  |  | file found, and \var{description} is a triple as contained in the list | 
					
						
							|  |  |  | returned by \code{get_suffixes} describing the kind of file found. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{init_builtin}{name} | 
					
						
							|  |  |  | Initialize the built-in module called \var{name} and return its module | 
					
						
							|  |  |  | object.  If the module was already initialized, it will be initialized | 
					
						
							| 
									
										
										
										
											1995-02-28 17:14:32 +00:00
										 |  |  | {\em again}.  A few modules cannot be initialized twice --- attempting | 
					
						
							| 
									
										
										
										
											1995-03-13 10:03:32 +00:00
										 |  |  | to initialize these again will raise an \code{ImportError} exception. | 
					
						
							|  |  |  | If there is no | 
					
						
							| 
									
										
										
										
											1995-01-10 10:51:08 +00:00
										 |  |  | built-in module called \var{name}, \code{None} is returned. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{init_frozen}{name} | 
					
						
							|  |  |  | Initialize the frozen module called \var{name} and return its module | 
					
						
							|  |  |  | object.  If the module was already initialized, it will be initialized | 
					
						
							|  |  |  | {\em again}.  If there is no frozen module called \var{name}, | 
					
						
							|  |  |  | \code{None} is returned.  (Frozen modules are modules written in | 
					
						
							|  |  |  | Python whose compiled byte-code object is incorporated into a | 
					
						
							|  |  |  | custom-built Python interpreter by Python's \code{freeze} utility. | 
					
						
							| 
									
										
										
										
											1995-03-28 13:35:14 +00:00
										 |  |  | See \code{Tools/freeze} for now.) | 
					
						
							| 
									
										
										
										
											1995-01-10 10:51:08 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{is_builtin}{name} | 
					
						
							|  |  |  | Return \code{1} if there is a built-in module called \var{name} which can be | 
					
						
							|  |  |  | initialized again.  Return \code{-1} if there is a built-in module | 
					
						
							|  |  |  | called \var{name} which cannot be initialized again (see | 
					
						
							|  |  |  | \code{init_builtin}).  Return \code{0} if there is no built-in module | 
					
						
							|  |  |  | called \var{name}. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{is_frozen}{name} | 
					
						
							|  |  |  | Return \code{1} if there is a frozen module (see \code{init_frozen}) | 
					
						
							|  |  |  | called \var{name}, \code{0} if there is no such module. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{load_compiled}{name\, pathname\, \optional{file}} | 
					
						
							|  |  |  | Load and initialize a module implemented as a byte-compiled code file | 
					
						
							|  |  |  | and return its module object.  If the module was already initialized, | 
					
						
							|  |  |  | it will be initialized {\em again}.  The \var{name} argument is used | 
					
						
							|  |  |  | to create or access a module object.  The \var{pathname} argument | 
					
						
							|  |  |  | points to the byte-compiled code file.  The optional \var{file} | 
					
						
							|  |  |  | argument is the byte-compiled code file, open for reading in binary | 
					
						
							| 
									
										
										
										
											1995-02-28 17:14:32 +00:00
										 |  |  | mode, from the beginning --- if not given, the function opens | 
					
						
							| 
									
										
										
										
											1995-01-10 10:51:08 +00:00
										 |  |  | \var{pathname}.  It must currently be a real file object, not a | 
					
						
							|  |  |  | user-defined class emulating a file. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{load_dynamic}{name\, pathname\, \optional{file}} | 
					
						
							|  |  |  | Load and initialize a module implemented as a dynamically loadable | 
					
						
							|  |  |  | shared library and return its module object.  If the module was | 
					
						
							|  |  |  | already initialized, it will be initialized {\em again}.  Some modules | 
					
						
							|  |  |  | don't like that and may raise an exception.  The \var{pathname} | 
					
						
							|  |  |  | argument must point to the shared library.  The \var{name} argument is | 
					
						
							|  |  |  | used to construct the name of the initialization function: an external | 
					
						
							|  |  |  | C function called \code{init\var{name}()} in the shared library is | 
					
						
							|  |  |  | called.  The optional \var{file} argment is ignored.  (Note: using | 
					
						
							|  |  |  | shared libraries is highly system dependent, and not all systems | 
					
						
							|  |  |  | support it.) | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{load_source}{name\, pathname\, \optional{file}} | 
					
						
							|  |  |  | Load and initialize a module implemented as a Python source file and | 
					
						
							|  |  |  | return its module object.  If the module was already initialized, it | 
					
						
							|  |  |  | will be initialized {\em again}.  The \var{name} argument is used to | 
					
						
							|  |  |  | create or access a module object.  The \var{pathname} argument points | 
					
						
							|  |  |  | to the source file.  The optional \var{file} argument is the source | 
					
						
							| 
									
										
										
										
											1995-02-28 17:14:32 +00:00
										 |  |  | file, open for reading as text, from the beginning --- if not given, | 
					
						
							| 
									
										
										
										
											1995-01-10 10:51:08 +00:00
										 |  |  | the function opens \var{pathname}.  It must currently be a real file | 
					
						
							|  |  |  | object, not a user-defined class emulating a file.  Note that if a | 
					
						
							|  |  |  | properly matching byte-compiled file (with suffix \code{.pyc}) exists, | 
					
						
							|  |  |  | it will be used instead of parsing the given source file. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{new_module}{name} | 
					
						
							|  |  |  | Return a new empty module object called \var{name}.  This object is | 
					
						
							|  |  |  | {\em not} inserted in \code{sys.modules}. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The following constants with integer values, defined in the module, | 
					
						
							|  |  |  | are used to indicate the search result of \code{imp.find_module}. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{datadesc}{SEARCH_ERROR} | 
					
						
							|  |  |  | The module was not found. | 
					
						
							|  |  |  | \end{datadesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{datadesc}{PY_SOURCE} | 
					
						
							|  |  |  | The module was found as a source file. | 
					
						
							|  |  |  | \end{datadesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{datadesc}{PY_COMPILED} | 
					
						
							|  |  |  | The module was found as a compiled code object file. | 
					
						
							|  |  |  | \end{datadesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{datadesc}{C_EXTENSION} | 
					
						
							|  |  |  | The module was found as dynamically loadable shared library. | 
					
						
							|  |  |  | \end{datadesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \subsection{Examples} | 
					
						
							|  |  |  | The following function emulates the default import statement: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							|  |  |  | import imp | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											1995-01-10 10:51:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-02-15 15:52:13 +00:00
										 |  |  | def __import__(name, globals=None, locals=None, fromlist=None): | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  |     # Fast path: see if the module has already been imported. | 
					
						
							|  |  |  |     if sys.modules.has_key(name): | 
					
						
							|  |  |  |         return sys.modules[name] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # If any of the following calls raises an exception, | 
					
						
							| 
									
										
										
										
											1995-04-10 11:34:00 +00:00
										 |  |  |     # there's a problem we can't handle -- let the caller handle it. | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # See if it's a built-in module. | 
					
						
							| 
									
										
										
										
											1995-01-10 10:51:08 +00:00
										 |  |  |     m = imp.init_builtin(name) | 
					
						
							|  |  |  |     if m: | 
					
						
							|  |  |  |         return m | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  |     # See if it's a frozen module. | 
					
						
							| 
									
										
										
										
											1995-01-10 10:51:08 +00:00
										 |  |  |     m = imp.init_frozen(name) | 
					
						
							|  |  |  |     if m: | 
					
						
							|  |  |  |         return m | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Search the default path (i.e. sys.path). | 
					
						
							|  |  |  |     fp, pathname, (suffix, mode, type) = imp.find_module(name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # See what we got. | 
					
						
							| 
									
										
										
										
											1995-07-07 23:01:27 +00:00
										 |  |  |     try: | 
					
						
							|  |  |  |         if type == imp.C_EXTENSION: | 
					
						
							|  |  |  |             return imp.load_dynamic(name, pathname) | 
					
						
							|  |  |  |         if type == imp.PY_SOURCE: | 
					
						
							|  |  |  |             return imp.load_source(name, pathname, fp) | 
					
						
							|  |  |  |         if type == imp.PY_COMPILED: | 
					
						
							|  |  |  |             return imp.load_compiled(name, pathname, fp) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Shouldn't get here at all. | 
					
						
							|  |  |  |         raise ImportError, '%s: unknown module type (%d)' % (name, type)
 | 
					
						
							|  |  |  |     finally: | 
					
						
							|  |  |  |         # Since we may exit via an exception, close fp explicitly. | 
					
						
							|  |  |  |         fp.close() | 
					
						
							| 
									
										
										
										
											1995-01-10 10:51:08 +00:00
										 |  |  | \end{verbatim} |