Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 1 | \section{Built-in Module \sectcode{imp}} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 2 | \label{module-imp} |
Guido van Rossum | 946805d | 1995-01-10 10:51:08 +0000 | [diff] [blame] | 3 | \bimodindex{imp} |
| 4 | \index{import} |
| 5 | |
Guido van Rossum | 6c4f003 | 1995-03-07 10:14:09 +0000 | [diff] [blame] | 6 | This module provides an interface to the mechanisms used to implement |
Guido van Rossum | 946805d | 1995-01-10 10:51:08 +0000 | [diff] [blame] | 7 | the \code{import} statement. It defines the following constants and |
| 8 | functions: |
| 9 | |
Guido van Rossum | 0bbbea1 | 1995-08-10 14:21:11 +0000 | [diff] [blame] | 10 | \renewcommand{\indexsubitem}{(in module imp)} |
Guido van Rossum | 946805d | 1995-01-10 10:51:08 +0000 | [diff] [blame] | 11 | |
| 12 | \begin{funcdesc}{get_magic}{} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 13 | Return the magic string value used to recognize byte-compiled code |
Guido van Rossum | 946805d | 1995-01-10 10:51:08 +0000 | [diff] [blame] | 14 | files (``\code{.pyc} files''). |
| 15 | \end{funcdesc} |
| 16 | |
| 17 | \begin{funcdesc}{get_suffixes}{} |
| 18 | Return a list of triples, each describing a particular type of file. |
| 19 | Each triple has the form \code{(\var{suffix}, \var{mode}, |
| 20 | \var{type})}, where \var{suffix} is a string to be appended to the |
| 21 | module name to form the filename to search for, \var{mode} is the mode |
| 22 | string to pass to the built-in \code{open} function to open the file |
| 23 | (this can be \code{'r'} for text files or \code{'rb'} for binary |
| 24 | files), and \var{type} is the file type, which has one of the values |
| 25 | \code{PY_SOURCE}, \code{PY_COMPILED} or \code{C_EXTENSION}, defined |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 26 | below. (System-dependent values may also be returned.) |
Guido van Rossum | 946805d | 1995-01-10 10:51:08 +0000 | [diff] [blame] | 27 | \end{funcdesc} |
| 28 | |
| 29 | \begin{funcdesc}{find_module}{name\, \optional{path}} |
| 30 | Try to find the module \var{name} on the search path \var{path}. The |
| 31 | default \var{path} is \code{sys.path}. The return value is a triple |
| 32 | \code{(\var{file}, \var{pathname}, \var{description})} where |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 33 | \var{file} is an open file object positioned at the beginning, |
| 34 | \var{pathname} is the pathname of the |
Guido van Rossum | 946805d | 1995-01-10 10:51:08 +0000 | [diff] [blame] | 35 | file found, and \var{description} is a triple as contained in the list |
| 36 | returned by \code{get_suffixes} describing the kind of file found. |
| 37 | \end{funcdesc} |
| 38 | |
| 39 | \begin{funcdesc}{init_builtin}{name} |
| 40 | Initialize the built-in module called \var{name} and return its module |
| 41 | object. If the module was already initialized, it will be initialized |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 42 | {\em again}. A few modules cannot be initialized twice --- attempting |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 43 | to initialize these again will raise an \code{ImportError} exception. |
| 44 | If there is no |
Guido van Rossum | 946805d | 1995-01-10 10:51:08 +0000 | [diff] [blame] | 45 | built-in module called \var{name}, \code{None} is returned. |
| 46 | \end{funcdesc} |
| 47 | |
| 48 | \begin{funcdesc}{init_frozen}{name} |
| 49 | Initialize the frozen module called \var{name} and return its module |
| 50 | object. If the module was already initialized, it will be initialized |
| 51 | {\em again}. If there is no frozen module called \var{name}, |
| 52 | \code{None} is returned. (Frozen modules are modules written in |
| 53 | Python whose compiled byte-code object is incorporated into a |
| 54 | custom-built Python interpreter by Python's \code{freeze} utility. |
Guido van Rossum | ecde781 | 1995-03-28 13:35:14 +0000 | [diff] [blame] | 55 | See \code{Tools/freeze} for now.) |
Guido van Rossum | 946805d | 1995-01-10 10:51:08 +0000 | [diff] [blame] | 56 | \end{funcdesc} |
| 57 | |
| 58 | \begin{funcdesc}{is_builtin}{name} |
| 59 | Return \code{1} if there is a built-in module called \var{name} which can be |
| 60 | initialized again. Return \code{-1} if there is a built-in module |
| 61 | called \var{name} which cannot be initialized again (see |
| 62 | \code{init_builtin}). Return \code{0} if there is no built-in module |
| 63 | called \var{name}. |
| 64 | \end{funcdesc} |
| 65 | |
| 66 | \begin{funcdesc}{is_frozen}{name} |
| 67 | Return \code{1} if there is a frozen module (see \code{init_frozen}) |
| 68 | called \var{name}, \code{0} if there is no such module. |
| 69 | \end{funcdesc} |
| 70 | |
Guido van Rossum | 4d20654 | 1996-06-26 19:21:24 +0000 | [diff] [blame] | 71 | \begin{funcdesc}{load_compiled}{name\, pathname\, file} |
Guido van Rossum | 946805d | 1995-01-10 10:51:08 +0000 | [diff] [blame] | 72 | Load and initialize a module implemented as a byte-compiled code file |
| 73 | and return its module object. If the module was already initialized, |
| 74 | it will be initialized {\em again}. The \var{name} argument is used |
| 75 | to create or access a module object. The \var{pathname} argument |
Guido van Rossum | 4d20654 | 1996-06-26 19:21:24 +0000 | [diff] [blame] | 76 | points to the byte-compiled code file. The \var{file} |
Guido van Rossum | 946805d | 1995-01-10 10:51:08 +0000 | [diff] [blame] | 77 | argument is the byte-compiled code file, open for reading in binary |
Guido van Rossum | 4d20654 | 1996-06-26 19:21:24 +0000 | [diff] [blame] | 78 | mode, from the beginning. |
| 79 | It must currently be a real file object, not a |
Guido van Rossum | 946805d | 1995-01-10 10:51:08 +0000 | [diff] [blame] | 80 | user-defined class emulating a file. |
| 81 | \end{funcdesc} |
| 82 | |
| 83 | \begin{funcdesc}{load_dynamic}{name\, pathname\, \optional{file}} |
| 84 | Load and initialize a module implemented as a dynamically loadable |
| 85 | shared library and return its module object. If the module was |
| 86 | already initialized, it will be initialized {\em again}. Some modules |
| 87 | don't like that and may raise an exception. The \var{pathname} |
| 88 | argument must point to the shared library. The \var{name} argument is |
| 89 | used to construct the name of the initialization function: an external |
| 90 | C function called \code{init\var{name}()} in the shared library is |
| 91 | called. The optional \var{file} argment is ignored. (Note: using |
| 92 | shared libraries is highly system dependent, and not all systems |
| 93 | support it.) |
| 94 | \end{funcdesc} |
| 95 | |
Guido van Rossum | 4d20654 | 1996-06-26 19:21:24 +0000 | [diff] [blame] | 96 | \begin{funcdesc}{load_source}{name\, pathname\, file} |
Guido van Rossum | 946805d | 1995-01-10 10:51:08 +0000 | [diff] [blame] | 97 | Load and initialize a module implemented as a Python source file and |
| 98 | return its module object. If the module was already initialized, it |
| 99 | will be initialized {\em again}. The \var{name} argument is used to |
| 100 | create or access a module object. The \var{pathname} argument points |
Guido van Rossum | 4d20654 | 1996-06-26 19:21:24 +0000 | [diff] [blame] | 101 | to the source file. The \var{file} argument is the source |
| 102 | file, open for reading as text, from the beginning. |
| 103 | It must currently be a real file |
Guido van Rossum | 946805d | 1995-01-10 10:51:08 +0000 | [diff] [blame] | 104 | object, not a user-defined class emulating a file. Note that if a |
| 105 | properly matching byte-compiled file (with suffix \code{.pyc}) exists, |
| 106 | it will be used instead of parsing the given source file. |
| 107 | \end{funcdesc} |
| 108 | |
| 109 | \begin{funcdesc}{new_module}{name} |
| 110 | Return a new empty module object called \var{name}. This object is |
| 111 | {\em not} inserted in \code{sys.modules}. |
| 112 | \end{funcdesc} |
| 113 | |
| 114 | The following constants with integer values, defined in the module, |
| 115 | are used to indicate the search result of \code{imp.find_module}. |
| 116 | |
| 117 | \begin{datadesc}{SEARCH_ERROR} |
| 118 | The module was not found. |
| 119 | \end{datadesc} |
| 120 | |
| 121 | \begin{datadesc}{PY_SOURCE} |
| 122 | The module was found as a source file. |
| 123 | \end{datadesc} |
| 124 | |
| 125 | \begin{datadesc}{PY_COMPILED} |
| 126 | The module was found as a compiled code object file. |
| 127 | \end{datadesc} |
| 128 | |
| 129 | \begin{datadesc}{C_EXTENSION} |
| 130 | The module was found as dynamically loadable shared library. |
| 131 | \end{datadesc} |
| 132 | |
| 133 | \subsection{Examples} |
| 134 | The following function emulates the default import statement: |
| 135 | |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 136 | \bcode\begin{verbatim} |
Guido van Rossum | 946805d | 1995-01-10 10:51:08 +0000 | [diff] [blame] | 137 | import imp |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 138 | import sys |
Guido van Rossum | 946805d | 1995-01-10 10:51:08 +0000 | [diff] [blame] | 139 | |
Guido van Rossum | 4f4c9b4 | 1995-02-15 15:52:13 +0000 | [diff] [blame] | 140 | def __import__(name, globals=None, locals=None, fromlist=None): |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 141 | # Fast path: see if the module has already been imported. |
| 142 | if sys.modules.has_key(name): |
| 143 | return sys.modules[name] |
Guido van Rossum | 946805d | 1995-01-10 10:51:08 +0000 | [diff] [blame] | 144 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 145 | # If any of the following calls raises an exception, |
Guido van Rossum | 96628a9 | 1995-04-10 11:34:00 +0000 | [diff] [blame] | 146 | # there's a problem we can't handle -- let the caller handle it. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 147 | |
| 148 | # See if it's a built-in module. |
Guido van Rossum | 946805d | 1995-01-10 10:51:08 +0000 | [diff] [blame] | 149 | m = imp.init_builtin(name) |
| 150 | if m: |
| 151 | return m |
| 152 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 153 | # See if it's a frozen module. |
Guido van Rossum | 946805d | 1995-01-10 10:51:08 +0000 | [diff] [blame] | 154 | m = imp.init_frozen(name) |
| 155 | if m: |
| 156 | return m |
| 157 | |
| 158 | # Search the default path (i.e. sys.path). |
Guido van Rossum | 946805d | 1995-01-10 10:51:08 +0000 | [diff] [blame] | 159 | fp, pathname, (suffix, mode, type) = imp.find_module(name) |
| 160 | |
| 161 | # See what we got. |
Guido van Rossum | d6ac380 | 1995-07-07 23:01:27 +0000 | [diff] [blame] | 162 | try: |
| 163 | if type == imp.C_EXTENSION: |
| 164 | return imp.load_dynamic(name, pathname) |
| 165 | if type == imp.PY_SOURCE: |
| 166 | return imp.load_source(name, pathname, fp) |
| 167 | if type == imp.PY_COMPILED: |
| 168 | return imp.load_compiled(name, pathname, fp) |
Guido van Rossum | 946805d | 1995-01-10 10:51:08 +0000 | [diff] [blame] | 169 | |
Guido van Rossum | d6ac380 | 1995-07-07 23:01:27 +0000 | [diff] [blame] | 170 | # Shouldn't get here at all. |
| 171 | raise ImportError, '%s: unknown module type (%d)' % (name, type) |
| 172 | finally: |
| 173 | # Since we may exit via an exception, close fp explicitly. |
| 174 | fp.close() |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 175 | \end{verbatim}\ecode |