home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 5
/
DATAFILE_PDCD5.iso
/
utilities
/
p
/
python
/
pyhtmldoc
/
i
/
imp
next >
Wrap
Text File
|
1996-11-14
|
7KB
|
120 lines
<TITLE>imp -- Python library reference</TITLE>
Next: <A HREF="../_/__builtin__" TYPE="Next">__builtin__</A>
Prev: <A HREF="../m/marshal" TYPE="Prev">marshal</A>
Up: <A HREF="../p/python_services" TYPE="Up">Python Services</A>
Top: <A HREF="../t/top" TYPE="Top">Top</A>
<H1>3.8. Built-in Module <CODE>imp</CODE></H1>
This module provides an interface to the mechanisms used to implement
the <CODE>import</CODE> statement. It defines the following constants and
functions:
<P>
<DL><DT><B>get_magic</B> () -- function of module imp<DD>
Return the magic string value used to recognize byte-compiled code
files (``<CODE>.pyc</CODE> files'').
</DL>
<DL><DT><B>get_suffixes</B> () -- function of module imp<DD>
Return a list of triples, each describing a particular type of file.
Each triple has the form <CODE>(<VAR>suffix</VAR>, <VAR>mode</VAR>,
<VAR>type</VAR>)</CODE>, where <VAR>suffix</VAR> is a string to be appended to the
module name to form the filename to search for, <VAR>mode</VAR> is the mode
string to pass to the built-in <CODE>open</CODE> function to open the file
(this can be <CODE>'r'</CODE> for text files or <CODE>'rb'</CODE> for binary
files), and <VAR>type</VAR> is the file type, which has one of the values
<CODE>PY_SOURCE</CODE>, <CODE>PY_COMPILED</CODE> or <CODE>C_EXTENSION</CODE>, defined
below. (System-dependent values may also be returned.)
</DL>
<DL><DT><B>find_module</B> (<VAR>name</VAR>, [<VAR>path</VAR>]) -- function of module imp<DD>
Try to find the module <VAR>name</VAR> on the search path <VAR>path</VAR>. The
default <VAR>path</VAR> is <CODE>sys.path</CODE>. The return value is a triple
<CODE>(<VAR>file</VAR>, <VAR>pathname</VAR>, <VAR>description</VAR>)</CODE> where
<VAR>file</VAR> is an open file object positioned at the beginning,
<VAR>pathname</VAR> is the pathname of the
file found, and <VAR>description</VAR> is a triple as contained in the list
returned by <CODE>get_suffixes</CODE> describing the kind of file found.
</DL>
<DL><DT><B>init_builtin</B> (<VAR>name</VAR>) -- function of module imp<DD>
Initialize the built-in module called <VAR>name</VAR> and return its module
object. If the module was already initialized, it will be initialized
<I>again</I>. A few modules cannot be initialized twice --- attempting
to initialize these again will raise an <CODE>ImportError</CODE> exception.
If there is no
built-in module called <VAR>name</VAR>, <CODE>None</CODE> is returned.
</DL>
<DL><DT><B>init_frozen</B> (<VAR>name</VAR>) -- function of module imp<DD>
Initialize the frozen module called <VAR>name</VAR> and return its module
object. If the module was already initialized, it will be initialized
<I>again</I>. If there is no frozen module called <VAR>name</VAR>,
<CODE>None</CODE> 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</CODE> utility.
See <CODE>Tools/freeze</CODE> for now.)
</DL>
<DL><DT><B>is_builtin</B> (<VAR>name</VAR>) -- function of module imp<DD>
Return <CODE>1</CODE> if there is a built-in module called <VAR>name</VAR> which can be
initialized again. Return <CODE>-1</CODE> if there is a built-in module
called <VAR>name</VAR> which cannot be initialized again (see
<CODE>init_builtin</CODE>). Return <CODE>0</CODE> if there is no built-in module
called <VAR>name</VAR>.
</DL>
<DL><DT><B>is_frozen</B> (<VAR>name</VAR>) -- function of module imp<DD>
Return <CODE>1</CODE> if there is a frozen module (see <CODE>init_frozen</CODE>)
called <VAR>name</VAR>, <CODE>0</CODE> if there is no such module.
</DL>
<DL><DT><B>load_compiled</B> (<VAR>name</VAR>, <VAR>pathname</VAR>, <VAR>file</VAR>) -- function of module imp<DD>
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 <I>again</I>. The <VAR>name</VAR> argument is used
to create or access a module object. The <VAR>pathname</VAR> argument
points to the byte-compiled code file. The <VAR>file</VAR>
argument is the byte-compiled code file, open for reading in binary
mode, from the beginning.
It must currently be a real file object, not a
user-defined class emulating a file.
</DL>
<DL><DT><B>load_dynamic</B> (<VAR>name</VAR>, <VAR>pathname</VAR>, [<VAR>file</VAR>]) -- function of module imp<DD>
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 <I>again</I>. Some modules
don't like that and may raise an exception. The <VAR>pathname</VAR>
argument must point to the shared library. The <VAR>name</VAR> argument is
used to construct the name of the initialization function: an external
C function called <CODE>init<VAR>name</VAR>()</CODE> in the shared library is
called. The optional <VAR>file</VAR> argment is ignored. (Note: using
shared libraries is highly system dependent, and not all systems
support it.)
</DL>
<DL><DT><B>load_source</B> (<VAR>name</VAR>, <VAR>pathname</VAR>, <VAR>file</VAR>) -- function of module imp<DD>
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 <I>again</I>. The <VAR>name</VAR> argument is used to
create or access a module object. The <VAR>pathname</VAR> argument points
to the source file. The <VAR>file</VAR> argument is the source
file, open for reading as text, from the beginning.
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</CODE>) exists,
it will be used instead of parsing the given source file.
</DL>
<DL><DT><B>new_module</B> (<VAR>name</VAR>) -- function of module imp<DD>
Return a new empty module object called <VAR>name</VAR>. This object is
<I>not</I> inserted in <CODE>sys.modules</CODE>.
</DL>
The following constants with integer values, defined in the module,
are used to indicate the search result of <CODE>imp.find_module</CODE>.
<P>
<DL><DT><B>SEARCH_ERROR</B> -- data of module imp<DD>
The module was not found.
</DL>
<DL><DT><B>PY_SOURCE</B> -- data of module imp<DD>
The module was found as a source file.
</DL>
<DL><DT><B>PY_COMPILED</B> -- data of module imp<DD>
The module was found as a compiled code object file.
</DL>
<DL><DT><B>C_EXTENSION</B> -- data of module imp<DD>
The module was found as dynamically loadable shared library.
</DL>
<H2>Menu</H2><DL COMPACT>
<DT><A HREF="../e/examples" TYPE=Menu>Examples</A>
<DD></DL>