⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 module-imp.html

📁 一本很好的python的说明书,适合对python感兴趣的人
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>3.17 imp -- Access the import internals</title>
<META NAME="description" CONTENT="3.17 imp -- Access the import internals">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="next" href="module-code.html" tppabs="http://www.python.org/doc/current/lib/module-code.html">
<LINK REL="previous" href="module-marshal.html" tppabs="http://www.python.org/doc/current/lib/module-marshal.html">
<LINK REL="up" href="python.html" tppabs="http://www.python.org/doc/current/lib/python.html">
<LINK REL="next" href="examples-imp.html" tppabs="http://www.python.org/doc/current/lib/examples-imp.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-marshal.html" tppabs="http://www.python.org/doc/current/lib/module-marshal.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A href="python.html" tppabs="http://www.python.org/doc/current/lib/python.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A href="examples-imp.html" tppabs="http://www.python.org/doc/current/lib/examples-imp.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
  alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
  alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
  alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="module-marshal.html" tppabs="http://www.python.org/doc/current/lib/module-marshal.html">3.16 marshal  </A>
<b class="navlabel">Up:</b> <a class="sectref" href="python.html" tppabs="http://www.python.org/doc/current/lib/python.html">3. Python Runtime Services</A>
<b class="navlabel">Next:</b> <a class="sectref" href="examples-imp.html" tppabs="http://www.python.org/doc/current/lib/examples-imp.html">3.17.1 Examples</A>
<br><hr></DIV>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION0051700000000000000000">
3.17 <tt class="module">imp</tt> --
         Access the <tt class="keyword">import</tt> internals</A>
</H1>

<P>


<P>
This module provides an interface to the mechanisms
used to implement the <tt class="keyword">import</tt> statement.  It defines the
following constants and functions:

<P>
<dl><dt><b><a name='l2h-453'><tt class='function'>get_magic</tt></a></b> ()
<dd>

Return the magic string value used to recognize byte-compiled code
files (<span class="file">.pyc</span> files).  (This value may be different for each
Python version.)
</dl>

<P>
<dl><dt><b><a name='l2h-454'><tt class='function'>get_suffixes</tt></a></b> ()
<dd>
Return a list of triples, each describing a particular type of module.
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 <tt class="function">open()</tt> 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
<tt class="constant">PY_SOURCE</tt>, <tt class="constant">PY_COMPILED</tt>, or
<tt class="constant">C_EXTENSION</tt>, described below.
</dl>

<P>
<dl><dt><b><a name='l2h-455'><tt class='function'>find_module</tt></a></b> (<var>name</var><big>[</big><var>, path</var><big>]</big>)
<dd>
Try to find the module <var>name</var> on the search path <var>path</var>.  If
<var>path</var> is a list of directory names, each directory is searched
for files with any of the suffixes returned by <tt class="function">get_suffixes()</tt>
above.  Invalid names in the list are silently ignored (but all list
items must be strings).  If <var>path</var> is omitted or <code>None</code>, the
list of directory names given by <code>sys.path</code> is searched, but
first it searches a few special places: it tries to find a built-in
module with the given name (<tt class="constant">C_BUILTIN</tt>), then a frozen module
(<tt class="constant">PY_FROZEN</tt>), and on some systems some other places are looked
in as well (on the Mac, it looks for a resource (<tt class="constant">PY_RESOURCE</tt>);
on Windows, it looks in the registry which may point to a specific
file).

<P>
If search is successful, 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 <tt class="function">get_suffixes()</tt> describing the kind of module found.
If the module does not live in a file, the returned <var>file</var> is
<code>None</code>, <var>filename</var> is the empty string, and the
<var>description</var> tuple contains empty strings for its suffix and
mode; the module type is as indicate in parentheses above.  If the
search is unsuccessful, <tt class="exception">ImportError</tt> is raised.  Other
exceptions indicate problems with the arguments or environment.

<P>
This function does not handle hierarchical module names (names
containing dots).  In order to find <var>P</var>.<var>M</var>, i.e., submodule
<var>M</var> of package <var>P</var>, use <tt class="function">find_module()</tt> and
<tt class="function">load_module()</tt> to find and load package <var>P</var>, and then use
<tt class="function">find_module()</tt> with the <var>path</var> argument set to
<code><var>P</var>.__path__</code>.  When <var>P</var> itself has a dotted name, apply
this recipe recursively.
</dl>

<P>
<dl><dt><b><a name='l2h-456'><tt class='function'>load_module</tt></a></b> (<var>name, file, filename, description</var>)
<dd>
Load a module that was previously found by <tt class="function">find_module()</tt> (or by
an otherwise conducted search yielding compatible results).  This
function does more than importing the module: if the module was
already imported, it is equivalent to a
<tt class="function">reload()</tt>!  The <var>name</var> argument
indicates the full module name (including the package name, if this is
a submodule of a package).  The <var>file</var> argument is an open file,
and <var>filename</var> is the corresponding file name; these can be
<code>None</code> and <code>''</code>, respectively, when the module is not being
loaded from a file.  The <var>description</var> argument is a tuple, as
would be returned by <tt class="function">get_suffixes()</tt>, describing what kind
of module must be loaded.

<P>
If the load is successful, the return value is the module object;
otherwise, an exception (usually <tt class="exception">ImportError</tt>) is raised.

<P>
<b>Important:</b> the caller is responsible for closing the
<var>file</var> argument, if it was not <code>None</code>, even when an exception
is raised.  This is best done using a <tt class="keyword">try</tt>
... <tt class="keyword">finally</tt> statement.
</dl>

<P>
<dl><dt><b><a name='l2h-457'><tt class='function'>new_module</tt></a></b> (<var>name</var>)
<dd>
Return a new empty module object called <var>name</var>.  This object is
<i>not</i> inserted in <code>sys.modules</code>.
</dl>

<P>
The following constants with integer values, defined in this module,
are used to indicate the search result of <tt class="function">find_module()</tt>.

<P>
<dl><dt><b><a name='l2h-458'><tt>PY_SOURCE</tt></a></b>
<dd>
The module was found as a source file.
</dl>

<P>
<dl><dt><b><a name='l2h-459'><tt>PY_COMPILED</tt></a></b>
<dd>
The module was found as a compiled code object file.
</dl>

<P>
<dl><dt><b><a name='l2h-460'><tt>C_EXTENSION</tt></a></b>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -