blob: 0f63524ec121dfef17612df590ec711d7c726950 [file] [log] [blame]
Guido van Rossum470be141995-03-17 16:07:09 +00001\section{Built-in Module \sectcode{imp}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-imp}
Guido van Rossum946805d1995-01-10 10:51:08 +00003\bimodindex{imp}
4\index{import}
5
Guido van Rossum6c4f0031995-03-07 10:14:09 +00006This module provides an interface to the mechanisms used to implement
Guido van Rossum946805d1995-01-10 10:51:08 +00007the \code{import} statement. It defines the following constants and
8functions:
9
Guido van Rossum0bbbea11995-08-10 14:21:11 +000010\renewcommand{\indexsubitem}{(in module imp)}
Guido van Rossum946805d1995-01-10 10:51:08 +000011
12\begin{funcdesc}{get_magic}{}
Guido van Rossum470be141995-03-17 16:07:09 +000013Return the magic string value used to recognize byte-compiled code
Guido van Rossum946805d1995-01-10 10:51:08 +000014files (``\code{.pyc} files'').
15\end{funcdesc}
16
17\begin{funcdesc}{get_suffixes}{}
18Return a list of triples, each describing a particular type of file.
19Each triple has the form \code{(\var{suffix}, \var{mode},
20\var{type})}, where \var{suffix} is a string to be appended to the
21module name to form the filename to search for, \var{mode} is the mode
22string 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
24files), 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 Rossum470be141995-03-17 16:07:09 +000026below. (System-dependent values may also be returned.)
Guido van Rossum946805d1995-01-10 10:51:08 +000027\end{funcdesc}
28
29\begin{funcdesc}{find_module}{name\, \optional{path}}
30Try to find the module \var{name} on the search path \var{path}. The
31default \var{path} is \code{sys.path}. The return value is a triple
32\code{(\var{file}, \var{pathname}, \var{description})} where
Guido van Rossum470be141995-03-17 16:07:09 +000033\var{file} is an open file object positioned at the beginning,
34\var{pathname} is the pathname of the
Guido van Rossum946805d1995-01-10 10:51:08 +000035file found, and \var{description} is a triple as contained in the list
36returned by \code{get_suffixes} describing the kind of file found.
37\end{funcdesc}
38
39\begin{funcdesc}{init_builtin}{name}
40Initialize the built-in module called \var{name} and return its module
41object. If the module was already initialized, it will be initialized
Guido van Rossum86751151995-02-28 17:14:32 +000042{\em again}. A few modules cannot be initialized twice --- attempting
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000043to initialize these again will raise an \code{ImportError} exception.
44If there is no
Guido van Rossum946805d1995-01-10 10:51:08 +000045built-in module called \var{name}, \code{None} is returned.
46\end{funcdesc}
47
48\begin{funcdesc}{init_frozen}{name}
49Initialize the frozen module called \var{name} and return its module
50object. 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
53Python whose compiled byte-code object is incorporated into a
54custom-built Python interpreter by Python's \code{freeze} utility.
Guido van Rossumecde7811995-03-28 13:35:14 +000055See \code{Tools/freeze} for now.)
Guido van Rossum946805d1995-01-10 10:51:08 +000056\end{funcdesc}
57
58\begin{funcdesc}{is_builtin}{name}
59Return \code{1} if there is a built-in module called \var{name} which can be
60initialized again. Return \code{-1} if there is a built-in module
61called \var{name} which cannot be initialized again (see
62\code{init_builtin}). Return \code{0} if there is no built-in module
63called \var{name}.
64\end{funcdesc}
65
66\begin{funcdesc}{is_frozen}{name}
67Return \code{1} if there is a frozen module (see \code{init_frozen})
68called \var{name}, \code{0} if there is no such module.
69\end{funcdesc}
70
Guido van Rossum4d206541996-06-26 19:21:24 +000071\begin{funcdesc}{load_compiled}{name\, pathname\, file}
Guido van Rossum946805d1995-01-10 10:51:08 +000072Load and initialize a module implemented as a byte-compiled code file
73and return its module object. If the module was already initialized,
74it will be initialized {\em again}. The \var{name} argument is used
75to create or access a module object. The \var{pathname} argument
Guido van Rossum4d206541996-06-26 19:21:24 +000076points to the byte-compiled code file. The \var{file}
Guido van Rossum946805d1995-01-10 10:51:08 +000077argument is the byte-compiled code file, open for reading in binary
Guido van Rossum4d206541996-06-26 19:21:24 +000078mode, from the beginning.
79It must currently be a real file object, not a
Guido van Rossum946805d1995-01-10 10:51:08 +000080user-defined class emulating a file.
81\end{funcdesc}
82
83\begin{funcdesc}{load_dynamic}{name\, pathname\, \optional{file}}
84Load and initialize a module implemented as a dynamically loadable
85shared library and return its module object. If the module was
86already initialized, it will be initialized {\em again}. Some modules
87don't like that and may raise an exception. The \var{pathname}
88argument must point to the shared library. The \var{name} argument is
89used to construct the name of the initialization function: an external
90C function called \code{init\var{name}()} in the shared library is
91called. The optional \var{file} argment is ignored. (Note: using
92shared libraries is highly system dependent, and not all systems
93support it.)
94\end{funcdesc}
95
Guido van Rossum4d206541996-06-26 19:21:24 +000096\begin{funcdesc}{load_source}{name\, pathname\, file}
Guido van Rossum946805d1995-01-10 10:51:08 +000097Load and initialize a module implemented as a Python source file and
98return its module object. If the module was already initialized, it
99will be initialized {\em again}. The \var{name} argument is used to
100create or access a module object. The \var{pathname} argument points
Guido van Rossum4d206541996-06-26 19:21:24 +0000101to the source file. The \var{file} argument is the source
102file, open for reading as text, from the beginning.
103It must currently be a real file
Guido van Rossum946805d1995-01-10 10:51:08 +0000104object, not a user-defined class emulating a file. Note that if a
105properly matching byte-compiled file (with suffix \code{.pyc}) exists,
106it will be used instead of parsing the given source file.
107\end{funcdesc}
108
109\begin{funcdesc}{new_module}{name}
110Return a new empty module object called \var{name}. This object is
111{\em not} inserted in \code{sys.modules}.
112\end{funcdesc}
113
114The following constants with integer values, defined in the module,
115are used to indicate the search result of \code{imp.find_module}.
116
117\begin{datadesc}{SEARCH_ERROR}
118The module was not found.
119\end{datadesc}
120
121\begin{datadesc}{PY_SOURCE}
122The module was found as a source file.
123\end{datadesc}
124
125\begin{datadesc}{PY_COMPILED}
126The module was found as a compiled code object file.
127\end{datadesc}
128
129\begin{datadesc}{C_EXTENSION}
130The module was found as dynamically loadable shared library.
131\end{datadesc}
132
133\subsection{Examples}
134The following function emulates the default import statement:
135
Guido van Rossume47da0a1997-07-17 16:34:52 +0000136\bcode\begin{verbatim}
Guido van Rossum946805d1995-01-10 10:51:08 +0000137import imp
Guido van Rossum470be141995-03-17 16:07:09 +0000138import sys
Guido van Rossum946805d1995-01-10 10:51:08 +0000139
Guido van Rossum4f4c9b41995-02-15 15:52:13 +0000140def __import__(name, globals=None, locals=None, fromlist=None):
Guido van Rossum470be141995-03-17 16:07:09 +0000141 # Fast path: see if the module has already been imported.
142 if sys.modules.has_key(name):
143 return sys.modules[name]
Guido van Rossum946805d1995-01-10 10:51:08 +0000144
Guido van Rossum470be141995-03-17 16:07:09 +0000145 # If any of the following calls raises an exception,
Guido van Rossum96628a91995-04-10 11:34:00 +0000146 # there's a problem we can't handle -- let the caller handle it.
Guido van Rossum470be141995-03-17 16:07:09 +0000147
148 # See if it's a built-in module.
Guido van Rossum946805d1995-01-10 10:51:08 +0000149 m = imp.init_builtin(name)
150 if m:
151 return m
152
Guido van Rossum470be141995-03-17 16:07:09 +0000153 # See if it's a frozen module.
Guido van Rossum946805d1995-01-10 10:51:08 +0000154 m = imp.init_frozen(name)
155 if m:
156 return m
157
158 # Search the default path (i.e. sys.path).
Guido van Rossum946805d1995-01-10 10:51:08 +0000159 fp, pathname, (suffix, mode, type) = imp.find_module(name)
160
161 # See what we got.
Guido van Rossumd6ac3801995-07-07 23:01:27 +0000162 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 Rossum946805d1995-01-10 10:51:08 +0000169
Guido van Rossumd6ac3801995-07-07 23:01:27 +0000170 # 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 Rossume47da0a1997-07-17 16:34:52 +0000175\end{verbatim}\ecode