Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > e870e6598e1c7e3918555a3d0ba5f3d4 > files > 663

python3-docs-3.1.1-2mdv2010.0.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>29.6. importlib – An implementation of import &mdash; Python v3.1.1 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.1.1',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v3.1.1 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v3.1.1 documentation" href="../index.html" />
    <link rel="up" title="29. Importing Modules" href="modules.html" />
    <link rel="next" title="30. Python Language Services" href="language.html" />
    <link rel="prev" title="29.5. runpy — Locating and executing Python modules" href="runpy.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="language.html" title="30. Python Language Services"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="runpy.html" title="29.5. runpy — Locating and executing Python modules"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v3.1.1 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="modules.html" accesskey="U">29. Importing Modules</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-importlib">
<h1>29.6. <tt class="xref docutils literal"><span class="pre">importlib</span></tt> &#8211; An implementation of <a class="reference external" href="../reference/simple_stmts.html#import"><tt class="xref docutils literal"><span class="pre">import</span></tt></a><a class="headerlink" href="#module-importlib" title="Permalink to this headline">¶</a></h1>
<p>
<span class="versionmodified">New in version 3.1.</span></p>
<div class="section" id="introduction">
<h2>29.6.1. Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
<p>The purpose of the <tt class="xref docutils literal"><span class="pre">importlib</span></tt> package is two-fold. One is to provide an
implementation of the <a class="reference external" href="../reference/simple_stmts.html#import"><tt class="xref docutils literal"><span class="pre">import</span></tt></a> statement (and thus, by extension, the
<a title="__import__" class="reference external" href="functions.html#__import__"><tt class="xref docutils literal"><span class="pre">__import__()</span></tt></a> function) in Python source code. This provides an
implementation of <a class="reference external" href="../reference/simple_stmts.html#import"><tt class="xref docutils literal"><span class="pre">import</span></tt></a> which is portable to any Python
interpreter. This also provides a reference implementation which is easier to
comprehend than one in a programming language other than Python.</p>
<p>Two, the components to implement <a class="reference external" href="../reference/simple_stmts.html#import"><tt class="xref docutils literal"><span class="pre">import</span></tt></a> can be exposed in this
package, making it easier for users to create their own custom objects (known
generically as an <a class="reference external" href="../glossary.html#term-importer"><em class="xref">importer</em></a>) to participate in the import process.
Details on providing custom importers can be found in <span class="target" id="index-354"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a>.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><a class="reference external" href="../reference/simple_stmts.html#import"><em>The import statement</em></a></dt>
<dd>The language reference for the <a class="reference external" href="../reference/simple_stmts.html#import"><tt class="xref docutils literal"><span class="pre">import</span></tt></a> statement.</dd>
<dt><a class="reference external" href="http://www.python.org/doc/essays/packages.html">Packages specification</a></dt>
<dd>Original specification of packages. Some semantics have changed since
the writing of this document (e.g. redirecting based on <tt class="xref xref docutils literal"><span class="pre">None</span></tt>
in <a title="sys.modules" class="reference external" href="sys.html#sys.modules"><tt class="xref docutils literal"><span class="pre">sys.modules</span></tt></a>).</dd>
<dt>The <a title="importlib.__import__" class="reference internal" href="#importlib.__import__"><tt class="xref docutils literal"><span class="pre">__import__()</span></tt></a> function</dt>
<dd>The built-in function for which the <a class="reference external" href="../reference/simple_stmts.html#import"><tt class="xref docutils literal"><span class="pre">import</span></tt></a> statement is
syntactic sugar.</dd>
<dt><span class="target" id="index-355"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-0235"><strong>PEP 235</strong></a></dt>
<dd>Import on Case-Insensitive Platforms</dd>
<dt><span class="target" id="index-356"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-0263"><strong>PEP 263</strong></a></dt>
<dd>Defining Python Source Code Encodings</dd>
<dt><span class="target" id="index-357"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a></dt>
<dd>New Import Hooks.</dd>
<dt><span class="target" id="index-358"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-0328"><strong>PEP 328</strong></a></dt>
<dd>Imports: Multi-Line and Absolute/Relative</dd>
<dt><span class="target" id="index-359"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-0366"><strong>PEP 366</strong></a></dt>
<dd>Main module explicit relative imports</dd>
<dt><span class="target" id="index-360"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-3128"><strong>PEP 3128</strong></a></dt>
<dd>Using UTF-8 as the Default Source Encoding</dd>
</dl>
</div>
</div>
<div class="section" id="functions">
<h2>29.6.2. Functions<a class="headerlink" href="#functions" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="importlib.__import__">
<tt class="descclassname">importlib.</tt><tt class="descname">__import__</tt><big>(</big><em>name</em>, <em>globals={}</em>, <em>locals={}</em>, <em>fromlist=list()</em>, <em>level=0</em><big>)</big><a class="headerlink" href="#importlib.__import__" title="Permalink to this definition">¶</a></dt>
<dd>An implementation of the built-in <a title="__import__" class="reference external" href="functions.html#__import__"><tt class="xref docutils literal"><span class="pre">__import__()</span></tt></a> function. See the
built-in function&#8217;s documentation for usage instructions.</dd></dl>

<dl class="function">
<dt id="importlib.import_module">
<tt class="descclassname">importlib.</tt><tt class="descname">import_module</tt><big>(</big><em>name</em>, <em>package=None</em><big>)</big><a class="headerlink" href="#importlib.import_module" title="Permalink to this definition">¶</a></dt>
<dd><p>Import a module. The <em>name</em> argument specifies what module to
import in absolute or relative terms
(e.g. either <tt class="docutils literal"><span class="pre">pkg.mod</span></tt> or <tt class="docutils literal"><span class="pre">..mod</span></tt>). If the name is
specified in relative terms, then the <em>package</em> argument must be set to
the name of the package which is to act as the anchor for resolving the
package name (e.g. <tt class="docutils literal"><span class="pre">import_module('..mod',</span> <span class="pre">'pkg.subpkg')</span></tt> will import
<tt class="docutils literal"><span class="pre">pkg.mod</span></tt>).</p>
<p>The <a title="importlib.import_module" class="reference internal" href="#importlib.import_module"><tt class="xref docutils literal"><span class="pre">import_module()</span></tt></a> function acts as a simplifying wrapper around
<a title="importlib.__import__" class="reference internal" href="#importlib.__import__"><tt class="xref docutils literal"><span class="pre">importlib.__import__()</span></tt></a>. This means all semantics of the function are
derived from <a title="importlib.__import__" class="reference internal" href="#importlib.__import__"><tt class="xref docutils literal"><span class="pre">importlib.__import__()</span></tt></a>, including requiring the package
from which an import is occurring to have been previously imported
(i.e., <em>package</em> must already be imported). The most important difference
is that <a title="importlib.import_module" class="reference internal" href="#importlib.import_module"><tt class="xref docutils literal"><span class="pre">import_module()</span></tt></a> returns the most nested package or module
that was imported (e.g. <tt class="docutils literal"><span class="pre">pkg.mod</span></tt>), while <a title="__import__" class="reference external" href="functions.html#__import__"><tt class="xref docutils literal"><span class="pre">__import__()</span></tt></a> returns the
top-level package or module (e.g. <tt class="docutils literal"><span class="pre">pkg</span></tt>).</p>
</dd></dl>

</div>
<div class="section" id="module-importlib.abc">
<h2>29.6.3. <tt class="xref docutils literal"><span class="pre">importlib.abc</span></tt> &#8211; Abstract base classes related to import<a class="headerlink" href="#module-importlib.abc" title="Permalink to this headline">¶</a></h2>
<p>The <tt class="xref docutils literal"><span class="pre">importlib.abc</span></tt> module contains all of the core abstract base classes
used by <a class="reference external" href="../reference/simple_stmts.html#import"><tt class="xref docutils literal"><span class="pre">import</span></tt></a>. Some subclasses of the core abstract base classes
are also provided to help in implementing the core ABCs.</p>
<dl class="class">
<dt id="importlib.abc.Finder">
<em class="property">
class </em><tt class="descclassname">importlib.abc.</tt><tt class="descname">Finder</tt><a class="headerlink" href="#importlib.abc.Finder" title="Permalink to this definition">¶</a></dt>
<dd><p>An abstract base class representing a <a class="reference external" href="../glossary.html#term-finder"><em class="xref">finder</em></a>.
See <span class="target" id="index-361"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> for the exact definition for a finder.</p>
<dl class="method">
<dt id="importlib.abc.Finder.find_module">
<tt class="descname">find_module</tt><big>(</big><em>fullname</em>, <em>path=None</em><big>)</big><a class="headerlink" href="#importlib.abc.Finder.find_module" title="Permalink to this definition">¶</a></dt>
<dd>An abstract method for finding a <a class="reference external" href="../glossary.html#term-loader"><em class="xref">loader</em></a> for the specified
module. If the <a class="reference external" href="../glossary.html#term-finder"><em class="xref">finder</em></a> is found on <a title="sys.meta_path" class="reference external" href="sys.html#sys.meta_path"><tt class="xref docutils literal"><span class="pre">sys.meta_path</span></tt></a> and the
module to be searched for is a subpackage or module then <em>path</em> will
be the value of <tt class="xref docutils literal"><span class="pre">__path__</span></tt> from the parent package. If a loader
cannot be found, <tt class="xref xref docutils literal"><span class="pre">None</span></tt> is returned.</dd></dl>

</dd></dl>

<dl class="class">
<dt id="importlib.abc.Loader">
<em class="property">
class </em><tt class="descclassname">importlib.abc.</tt><tt class="descname">Loader</tt><a class="headerlink" href="#importlib.abc.Loader" title="Permalink to this definition">¶</a></dt>
<dd><p>An abstract base class for a <a class="reference external" href="../glossary.html#term-loader"><em class="xref">loader</em></a>.
See <span class="target" id="index-362"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> for the exact definition for a loader.</p>
<dl class="method">
<dt id="importlib.abc.Loader.load_module">
<tt class="descname">load_module</tt><big>(</big><em>fullname</em><big>)</big><a class="headerlink" href="#importlib.abc.Loader.load_module" title="Permalink to this definition">¶</a></dt>
<dd><p>An abstract method for loading a module. If the module cannot be
loaded, <a title="exceptions.ImportError" class="reference external" href="exceptions.html#exceptions.ImportError"><tt class="xref docutils literal"><span class="pre">ImportError</span></tt></a> is raised, otherwise the loaded module is
returned.</p>
<p>If the requested module already exists in <a title="sys.modules" class="reference external" href="sys.html#sys.modules"><tt class="xref docutils literal"><span class="pre">sys.modules</span></tt></a>, that
module should be used and reloaded.
Otherwise the loader should create a new module and insert it into
<a title="sys.modules" class="reference external" href="sys.html#sys.modules"><tt class="xref docutils literal"><span class="pre">sys.modules</span></tt></a> before any loading begins, to prevent recursion
from the import. If the loader inserted a module and the load fails, it
must be removed by the loader from <a title="sys.modules" class="reference external" href="sys.html#sys.modules"><tt class="xref docutils literal"><span class="pre">sys.modules</span></tt></a>; modules already
in <a title="sys.modules" class="reference external" href="sys.html#sys.modules"><tt class="xref docutils literal"><span class="pre">sys.modules</span></tt></a> before the loader began execution should be left
alone. The <a title="importlib.util.module_for_loader" class="reference internal" href="#importlib.util.module_for_loader"><tt class="xref docutils literal"><span class="pre">importlib.util.module_for_loader()</span></tt></a> decorator handles
all of these details.</p>
<p>The loader should set several attributes on the module.
(Note that some of these attributes can change when a module is
reloaded.)</p>
<ul>
<li><dl class="first docutils">
<dt><tt class="xref docutils literal"><span class="pre">__name__</span></tt></dt>
<dd><p class="first last">The name of the module.</p>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt><tt class="xref docutils literal"><span class="pre">__file__</span></tt></dt>
<dd><p class="first last">The path to where the module data is stored (not set for built-in
modules).</p>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt><tt class="xref docutils literal"><span class="pre">__path__</span></tt></dt>
<dd><p class="first last">A list of strings specifying the search path within a
package. This attribute is not set on modules.</p>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt><tt class="xref docutils literal"><span class="pre">__package__</span></tt></dt>
<dd><p class="first last">The parent package for the module/package. If the module is
top-level then it has a value of the empty string. The
<a title="importlib.util.set_package" class="reference internal" href="#importlib.util.set_package"><tt class="xref docutils literal"><span class="pre">importlib.util.set_package()</span></tt></a> decorator can handle the details
for <tt class="xref docutils literal"><span class="pre">__package__</span></tt>.</p>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt><tt class="xref docutils literal"><span class="pre">__loader__</span></tt></dt>
<dd><p class="first last">The loader used to load the module.
(This is not set by the built-in import machinery,
but it should be set whenever a <a class="reference external" href="../glossary.html#term-loader"><em class="xref">loader</em></a> is used.)</p>
</dd>
</dl>
</li>
</ul>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="importlib.abc.ResourceLoader">
<em class="property">
class </em><tt class="descclassname">importlib.abc.</tt><tt class="descname">ResourceLoader</tt><a class="headerlink" href="#importlib.abc.ResourceLoader" title="Permalink to this definition">¶</a></dt>
<dd><p>An abstract base class for a <a class="reference external" href="../glossary.html#term-loader"><em class="xref">loader</em></a> which implements the optional
<span class="target" id="index-363"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> protocol for loading arbitrary resources from the storage
back-end.</p>
<dl class="method">
<dt id="importlib.abc.ResourceLoader.get_data">
<tt class="descname">get_data</tt><big>(</big><em>path</em><big>)</big><a class="headerlink" href="#importlib.abc.ResourceLoader.get_data" title="Permalink to this definition">¶</a></dt>
<dd>An abstract method to return the bytes for the data located at <em>path</em>.
Loaders that have a file-like storage back-end
that allows storing arbitrary data
can implement this abstract method to give direct access
to the data stored. <a title="exceptions.IOError" class="reference external" href="exceptions.html#exceptions.IOError"><tt class="xref docutils literal"><span class="pre">IOError</span></tt></a> is to be raised if the <em>path</em> cannot
be found. The <em>path</em> is expected to be constructed using a module&#8217;s
<tt class="xref docutils literal"><span class="pre">__file__</span></tt> attribute or an item from a package&#8217;s <tt class="xref docutils literal"><span class="pre">__path__</span></tt>.</dd></dl>

</dd></dl>

<dl class="class">
<dt id="importlib.abc.InspectLoader">
<em class="property">
class </em><tt class="descclassname">importlib.abc.</tt><tt class="descname">InspectLoader</tt><a class="headerlink" href="#importlib.abc.InspectLoader" title="Permalink to this definition">¶</a></dt>
<dd><p>An abstract base class for a <a class="reference external" href="../glossary.html#term-loader"><em class="xref">loader</em></a> which implements the optional
<span class="target" id="index-364"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> protocol for loaders that inspect modules.</p>
<dl class="method">
<dt id="importlib.abc.InspectLoader.get_code">
<tt class="descname">get_code</tt><big>(</big><em>fullname</em><big>)</big><a class="headerlink" href="#importlib.abc.InspectLoader.get_code" title="Permalink to this definition">¶</a></dt>
<dd>An abstract method to return the <tt class="xref docutils literal"><span class="pre">code</span></tt> object for a module.
<tt class="xref xref docutils literal"><span class="pre">None</span></tt> is returned if the module does not have a code object
(e.g. built-in module).  <a title="exceptions.ImportError" class="reference external" href="exceptions.html#exceptions.ImportError"><tt class="xref docutils literal"><span class="pre">ImportError</span></tt></a> is raised if loader cannot
find the requested module.</dd></dl>

<dl class="method">
<dt id="importlib.abc.InspectLoader.get_source">
<tt class="descname">get_source</tt><big>(</big><em>fullname</em><big>)</big><a class="headerlink" href="#importlib.abc.InspectLoader.get_source" title="Permalink to this definition">¶</a></dt>
<dd>An abstract method to return the source of a module. It is returned as
a text string with universal newlines. Returns <tt class="xref xref docutils literal"><span class="pre">None</span></tt> if no
source is available (e.g. a built-in module). Raises <a title="exceptions.ImportError" class="reference external" href="exceptions.html#exceptions.ImportError"><tt class="xref docutils literal"><span class="pre">ImportError</span></tt></a>
if the loader cannot find the module specified.</dd></dl>

<dl class="method">
<dt id="importlib.abc.InspectLoader.is_package">
<tt class="descname">is_package</tt><big>(</big><em>fullname</em><big>)</big><a class="headerlink" href="#importlib.abc.InspectLoader.is_package" title="Permalink to this definition">¶</a></dt>
<dd>An abstract method to return a true value if the module is a package, a
false value otherwise. <a title="exceptions.ImportError" class="reference external" href="exceptions.html#exceptions.ImportError"><tt class="xref docutils literal"><span class="pre">ImportError</span></tt></a> is raised if the
<a class="reference external" href="../glossary.html#term-loader"><em class="xref">loader</em></a> cannot find the module.</dd></dl>

</dd></dl>

<dl class="class">
<dt id="importlib.abc.PyLoader">
<em class="property">
class </em><tt class="descclassname">importlib.abc.</tt><tt class="descname">PyLoader</tt><a class="headerlink" href="#importlib.abc.PyLoader" title="Permalink to this definition">¶</a></dt>
<dd><p>An abstract base class inheriting from <a title="importlib.abc.InspectLoader" class="reference internal" href="#importlib.abc.InspectLoader"><tt class="xref docutils literal"><span class="pre">importlib.abc.InspectLoader</span></tt></a>
and <a title="importlib.abc.ResourceLoader" class="reference internal" href="#importlib.abc.ResourceLoader"><tt class="xref docutils literal"><span class="pre">importlib.abc.ResourceLoader</span></tt></a> designed to ease the loading of
Python source modules (bytecode is not handled; see
<a title="importlib.abc.PyPycLoader" class="reference internal" href="#importlib.abc.PyPycLoader"><tt class="xref docutils literal"><span class="pre">importlib.abc.PyPycLoader</span></tt></a> for a source/bytecode ABC). A subclass
implementing this ABC will only need to worry about exposing how the source
code is stored; all other details for loading Python source code will be
handled by the concrete implementations of key methods.</p>
<dl class="method">
<dt id="importlib.abc.PyLoader.source_path">
<tt class="descname">source_path</tt><big>(</big><em>fullname</em><big>)</big><a class="headerlink" href="#importlib.abc.PyLoader.source_path" title="Permalink to this definition">¶</a></dt>
<dd>An abstract method that returns the path to the source code for a
module. Should return <tt class="xref xref docutils literal"><span class="pre">None</span></tt> if there is no source code.
<a title="exceptions.ImportError" class="reference external" href="exceptions.html#exceptions.ImportError"><tt class="xref docutils literal"><span class="pre">ImportError</span></tt></a> if the module cannot be found.</dd></dl>

<dl class="method">
<dt id="importlib.abc.PyLoader.load_module">
<tt class="descname">load_module</tt><big>(</big><em>fullname</em><big>)</big><a class="headerlink" href="#importlib.abc.PyLoader.load_module" title="Permalink to this definition">¶</a></dt>
<dd>A concrete implementation of <a title="importlib.abc.Loader.load_module" class="reference internal" href="#importlib.abc.Loader.load_module"><tt class="xref docutils literal"><span class="pre">importlib.abc.Loader.load_module()</span></tt></a>
that loads Python source code. All needed information comes from the
abstract methods required by this ABC. The only pertinent assumption
made by this method is that when loading a package
<tt class="xref docutils literal"><span class="pre">__path__</span></tt> is set to <tt class="docutils literal"><span class="pre">[os.path.dirname(__file__)]</span></tt>.</dd></dl>

<dl class="method">
<dt id="importlib.abc.PyLoader.get_code">
<tt class="descname">get_code</tt><big>(</big><em>fullname</em><big>)</big><a class="headerlink" href="#importlib.abc.PyLoader.get_code" title="Permalink to this definition">¶</a></dt>
<dd>A concrete implementation of
<a title="importlib.abc.InspectLoader.get_code" class="reference internal" href="#importlib.abc.InspectLoader.get_code"><tt class="xref docutils literal"><span class="pre">importlib.abc.InspectLoader.get_code()</span></tt></a> that creates code objects
from Python source code, by requesting the source code (using
<a title="importlib.abc.PyLoader.source_path" class="reference internal" href="#importlib.abc.PyLoader.source_path"><tt class="xref docutils literal"><span class="pre">source_path()</span></tt></a> and <tt class="xref docutils literal"><span class="pre">get_data()</span></tt>), converting it to standard
newlines, and compiling it with the built-in <a title="compile" class="reference external" href="functions.html#compile"><tt class="xref docutils literal"><span class="pre">compile()</span></tt></a> function.</dd></dl>

<dl class="method">
<dt id="importlib.abc.PyLoader.get_source">
<tt class="descname">get_source</tt><big>(</big><em>fullname</em><big>)</big><a class="headerlink" href="#importlib.abc.PyLoader.get_source" title="Permalink to this definition">¶</a></dt>
<dd>A concrete implementation of
<a title="importlib.abc.InspectLoader.get_source" class="reference internal" href="#importlib.abc.InspectLoader.get_source"><tt class="xref docutils literal"><span class="pre">importlib.abc.InspectLoader.get_source()</span></tt></a>. Uses
<a title="importlib.abc.ResourceLoader.get_data" class="reference internal" href="#importlib.abc.ResourceLoader.get_data"><tt class="xref docutils literal"><span class="pre">importlib.abc.ResourceLoader.get_data()</span></tt></a> and <a title="importlib.abc.PyLoader.source_path" class="reference internal" href="#importlib.abc.PyLoader.source_path"><tt class="xref docutils literal"><span class="pre">source_path()</span></tt></a> to
get the source code.  It tries to guess the source encoding using
<a title="tokenize.detect_encoding" class="reference external" href="tokenize.html#tokenize.detect_encoding"><tt class="xref docutils literal"><span class="pre">tokenize.detect_encoding()</span></tt></a>.</dd></dl>

</dd></dl>

<dl class="class">
<dt id="importlib.abc.PyPycLoader">
<em class="property">
class </em><tt class="descclassname">importlib.abc.</tt><tt class="descname">PyPycLoader</tt><a class="headerlink" href="#importlib.abc.PyPycLoader" title="Permalink to this definition">¶</a></dt>
<dd><p>An abstract base class inheriting from <a title="importlib.abc.PyLoader" class="reference internal" href="#importlib.abc.PyLoader"><tt class="xref docutils literal"><span class="pre">importlib.abc.PyLoader</span></tt></a>.
This ABC is meant to help in creating loaders that support both Python
source and bytecode.</p>
<dl class="method">
<dt id="importlib.abc.PyPycLoader.source_mtime">
<tt class="descname">source_mtime</tt><big>(</big><em>fullname</em><big>)</big><a class="headerlink" href="#importlib.abc.PyPycLoader.source_mtime" title="Permalink to this definition">¶</a></dt>
<dd>An abstract method which returns the modification time for the source
code of the specified module. The modification time should be an
integer. If there is no source code, return <tt class="xref xref docutils literal"><span class="pre">None</span></tt>. If the
module cannot be found then <a title="exceptions.ImportError" class="reference external" href="exceptions.html#exceptions.ImportError"><tt class="xref docutils literal"><span class="pre">ImportError</span></tt></a> is raised.</dd></dl>

<dl class="method">
<dt id="importlib.abc.PyPycLoader.bytecode_path">
<tt class="descname">bytecode_path</tt><big>(</big><em>fullname</em><big>)</big><a class="headerlink" href="#importlib.abc.PyPycLoader.bytecode_path" title="Permalink to this definition">¶</a></dt>
<dd>An abstract method which returns the path to the bytecode for the
specified module, if it exists. It returns <tt class="xref xref docutils literal"><span class="pre">None</span></tt>
if no bytecode exists (yet).
Raises <a title="exceptions.ImportError" class="reference external" href="exceptions.html#exceptions.ImportError"><tt class="xref docutils literal"><span class="pre">ImportError</span></tt></a> if the module is not found.</dd></dl>

<dl class="method">
<dt id="importlib.abc.PyPycLoader.write_bytecode">
<tt class="descname">write_bytecode</tt><big>(</big><em>fullname</em>, <em>bytecode</em><big>)</big><a class="headerlink" href="#importlib.abc.PyPycLoader.write_bytecode" title="Permalink to this definition">¶</a></dt>
<dd>An abstract method which has the loader write <em>bytecode</em> for future
use. If the bytecode is written, return <tt class="xref xref docutils literal"><span class="pre">True</span></tt>. Return
<tt class="xref xref docutils literal"><span class="pre">False</span></tt> if the bytecode could not be written. This method
should not be called if <a title="sys.dont_write_bytecode" class="reference external" href="sys.html#sys.dont_write_bytecode"><tt class="xref docutils literal"><span class="pre">sys.dont_write_bytecode</span></tt></a> is true.
The <em>bytecode</em> argument should be a bytes string or bytes array.</dd></dl>

</dd></dl>

</div>
<div class="section" id="module-importlib.machinery">
<h2>29.6.4. <tt class="xref docutils literal"><span class="pre">importlib.machinery</span></tt> &#8211; Importers and path hooks<a class="headerlink" href="#module-importlib.machinery" title="Permalink to this headline">¶</a></h2>
<p>This module contains the various objects that help <a class="reference external" href="../reference/simple_stmts.html#import"><tt class="xref docutils literal"><span class="pre">import</span></tt></a>
find and load modules.</p>
<dl class="class">
<dt id="importlib.machinery.BuiltinImporter">
<em class="property">
class </em><tt class="descclassname">importlib.machinery.</tt><tt class="descname">BuiltinImporter</tt><a class="headerlink" href="#importlib.machinery.BuiltinImporter" title="Permalink to this definition">¶</a></dt>
<dd><p>An <a class="reference external" href="../glossary.html#term-importer"><em class="xref">importer</em></a> for built-in modules. All known built-in modules are
listed in <a title="sys.builtin_module_names" class="reference external" href="sys.html#sys.builtin_module_names"><tt class="xref docutils literal"><span class="pre">sys.builtin_module_names</span></tt></a>. This class implements the
<a title="importlib.abc.Finder" class="reference internal" href="#importlib.abc.Finder"><tt class="xref docutils literal"><span class="pre">importlib.abc.Finder</span></tt></a> and <a title="importlib.abc.InspectLoader" class="reference internal" href="#importlib.abc.InspectLoader"><tt class="xref docutils literal"><span class="pre">importlib.abc.InspectLoader</span></tt></a>
ABCs.</p>
<p>Only class methods are defined by this class to alleviate the need for
instantiation.</p>
</dd></dl>

<dl class="class">
<dt id="importlib.machinery.FrozenImporter">
<em class="property">
class </em><tt class="descclassname">importlib.machinery.</tt><tt class="descname">FrozenImporter</tt><a class="headerlink" href="#importlib.machinery.FrozenImporter" title="Permalink to this definition">¶</a></dt>
<dd><p>An <a class="reference external" href="../glossary.html#term-importer"><em class="xref">importer</em></a> for frozen modules. This class implements the
<a title="importlib.abc.Finder" class="reference internal" href="#importlib.abc.Finder"><tt class="xref docutils literal"><span class="pre">importlib.abc.Finder</span></tt></a> and <a title="importlib.abc.InspectLoader" class="reference internal" href="#importlib.abc.InspectLoader"><tt class="xref docutils literal"><span class="pre">importlib.abc.InspectLoader</span></tt></a>
ABCs.</p>
<p>Only class methods are defined by this class to alleviate the need for
instantiation.</p>
</dd></dl>

<dl class="class">
<dt id="importlib.machinery.PathFinder">
<em class="property">
class </em><tt class="descclassname">importlib.machinery.</tt><tt class="descname">PathFinder</tt><a class="headerlink" href="#importlib.machinery.PathFinder" title="Permalink to this definition">¶</a></dt>
<dd><p><a class="reference external" href="../glossary.html#term-finder"><em class="xref">Finder</em></a> for <a title="sys.path" class="reference external" href="sys.html#sys.path"><tt class="xref docutils literal"><span class="pre">sys.path</span></tt></a>. This class implements the
<a title="importlib.abc.Finder" class="reference internal" href="#importlib.abc.Finder"><tt class="xref docutils literal"><span class="pre">importlib.abc.Finder</span></tt></a> ABC.</p>
<p>This class does not perfectly mirror the semantics of <a class="reference external" href="../reference/simple_stmts.html#import"><tt class="xref docutils literal"><span class="pre">import</span></tt></a> in
terms of <a title="sys.path" class="reference external" href="sys.html#sys.path"><tt class="xref docutils literal"><span class="pre">sys.path</span></tt></a>. No implicit path hooks are assumed for
simplification of the class and its semantics.</p>
<p>Only class method are defined by this class to alleviate the need for
instantiation.</p>
<dl class="classmethod">
<dt id="importlib.machinery.PathFinder.find_module">
<em class="property">
classmethod </em><tt class="descname">find_module</tt><big>(</big><em>fullname</em>, <em>path=None</em><big>)</big><a class="headerlink" href="#importlib.machinery.PathFinder.find_module" title="Permalink to this definition">¶</a></dt>
<dd>Class method that attempts to find a <a class="reference external" href="../glossary.html#term-loader"><em class="xref">loader</em></a> for the module
specified by <em>fullname</em> on <a title="sys.path" class="reference external" href="sys.html#sys.path"><tt class="xref docutils literal"><span class="pre">sys.path</span></tt></a> or, if defined, on
<em>path</em>. For each path entry that is searched,
<a title="sys.path_importer_cache" class="reference external" href="sys.html#sys.path_importer_cache"><tt class="xref docutils literal"><span class="pre">sys.path_importer_cache</span></tt></a> is checked. If an non-false object is
found then it is used as the <a class="reference external" href="../glossary.html#term-finder"><em class="xref">finder</em></a> to look for the module
being searched for. If no entry is found in
<a title="sys.path_importer_cache" class="reference external" href="sys.html#sys.path_importer_cache"><tt class="xref docutils literal"><span class="pre">sys.path_importer_cache</span></tt></a>, then <a title="sys.path_hooks" class="reference external" href="sys.html#sys.path_hooks"><tt class="xref docutils literal"><span class="pre">sys.path_hooks</span></tt></a> is
searched for a finder for the path entry and, if found, is stored in
<a title="sys.path_importer_cache" class="reference external" href="sys.html#sys.path_importer_cache"><tt class="xref docutils literal"><span class="pre">sys.path_importer_cache</span></tt></a> along with being queried about the
module. If no finder is ever found then <tt class="xref xref docutils literal"><span class="pre">None</span></tt> is returned.</dd></dl>

</dd></dl>

</div>
<div class="section" id="module-importlib.util">
<h2>29.6.5. <tt class="xref docutils literal"><span class="pre">importlib.util</span></tt> &#8211; Utility code for importers<a class="headerlink" href="#module-importlib.util" title="Permalink to this headline">¶</a></h2>
<p>This module contains the various objects that help in the construction of
an <a class="reference external" href="../glossary.html#term-importer"><em class="xref">importer</em></a>.</p>
<dl class="function">
<dt id="importlib.util.module_for_loader">
<tt class="descclassname">importlib.util.</tt><tt class="descname">module_for_loader</tt><big>(</big><em>method</em><big>)</big><a class="headerlink" href="#importlib.util.module_for_loader" title="Permalink to this definition">¶</a></dt>
<dd><p>A <a class="reference external" href="../glossary.html#term-decorator"><em class="xref">decorator</em></a> for a <a class="reference external" href="../glossary.html#term-loader"><em class="xref">loader</em></a> method,
to handle selecting the proper
module object to load with. The decorated method is expected to have a call
signature taking two positional arguments
(e.g. <tt class="docutils literal"><span class="pre">load_module(self,</span> <span class="pre">module)</span></tt>) for which the second argument
will be the module <strong>object</strong> to be used by the loader.
Note that the decorator
will not work on static methods because of the assumption of two
arguments.</p>
<p>The decorated method will take in the <strong>name</strong> of the module to be loaded
as expected for a <a class="reference external" href="../glossary.html#term-loader"><em class="xref">loader</em></a>. If the module is not found in
<a title="sys.modules" class="reference external" href="sys.html#sys.modules"><tt class="xref docutils literal"><span class="pre">sys.modules</span></tt></a> then a new one is constructed with its
<tt class="xref docutils literal"><span class="pre">__name__</span></tt> attribute set. Otherwise the module found in
<a title="sys.modules" class="reference external" href="sys.html#sys.modules"><tt class="xref docutils literal"><span class="pre">sys.modules</span></tt></a> will be passed into the method. If an
exception is raised by the decorated method and a module was added to
<a title="sys.modules" class="reference external" href="sys.html#sys.modules"><tt class="xref docutils literal"><span class="pre">sys.modules</span></tt></a> it will be removed to prevent a partially initialized
module from being in left in <a title="sys.modules" class="reference external" href="sys.html#sys.modules"><tt class="xref docutils literal"><span class="pre">sys.modules</span></tt></a>. If the module was already
in <a title="sys.modules" class="reference external" href="sys.html#sys.modules"><tt class="xref docutils literal"><span class="pre">sys.modules</span></tt></a> then it is left alone.</p>
<p>Use of this decorator handles all the details of which module object a
loader should initialize as specified by <span class="target" id="index-365"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a>.</p>
</dd></dl>

<dl class="function">
<dt id="importlib.util.set_loader">
<tt class="descclassname">importlib.util.</tt><tt class="descname">set_loader</tt><big>(</big><em>fxn</em><big>)</big><a class="headerlink" href="#importlib.util.set_loader" title="Permalink to this definition">¶</a></dt>
<dd>A <a class="reference external" href="../glossary.html#term-decorator"><em class="xref">decorator</em></a> for a <a class="reference external" href="../glossary.html#term-loader"><em class="xref">loader</em></a> method,
to set the <tt class="xref docutils literal"><span class="pre">__loader__</span></tt>
attribute on loaded modules. If the attribute is already set the decorator
does nothing. It is assumed that the first positional argument to the
wrapped method is what <tt class="xref docutils literal"><span class="pre">__loader__</span></tt> should be set to.</dd></dl>

<dl class="function">
<dt id="importlib.util.set_package">
<tt class="descclassname">importlib.util.</tt><tt class="descname">set_package</tt><big>(</big><em>fxn</em><big>)</big><a class="headerlink" href="#importlib.util.set_package" title="Permalink to this definition">¶</a></dt>
<dd><p>A <a class="reference external" href="../glossary.html#term-decorator"><em class="xref">decorator</em></a> for a <a class="reference external" href="../glossary.html#term-loader"><em class="xref">loader</em></a> to set the <tt class="xref docutils literal"><span class="pre">__package__</span></tt>
attribute on the module returned by the loader. If <tt class="xref docutils literal"><span class="pre">__package__</span></tt> is
set and has a value other than <tt class="xref xref docutils literal"><span class="pre">None</span></tt> it will not be changed.
Note that the module returned by the loader is what has the attribute
set on and not the module found in <a title="sys.modules" class="reference external" href="sys.html#sys.modules"><tt class="xref docutils literal"><span class="pre">sys.modules</span></tt></a>.</p>
<p>Reliance on this decorator is discouraged when it is possible to set
<tt class="xref docutils literal"><span class="pre">__package__</span></tt> before the execution of the code is possible. By
setting it before the code for the module is executed it allows the
attribute to be used at the global level of the module during
initialization.</p>
</dd></dl>

</div>
<div class="section" id="example">
<h2>29.6.6. Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h2>
<p>Below is an example meta path importer that uses a dict for back-end storage
for source code. While not an optimal solution &#8211; manipulations of
<tt class="xref docutils literal"><span class="pre">__path__</span></tt> on packages does not influence import &#8211; it does illustrate
what little is required to implement an importer.</p>
<div class="highlight-python3"><pre>"""An importer where source is stored in a dict."""
from importlib import abc


class DictImporter(abc.Finder, abc.PyLoader):

    """A meta path importer that stores source code in a dict.

    The keys are the module names -- packages must end in ``.__init__``.
    The values must be something that can be passed to 'bytes'.

    """

    def __init__(self, memory):
        """Store the dict."""
        self.memory = memory

    def contains(self, name):
        """See if a module or package is in the dict."""
        if name in self.memory:
            return name
        package_name = '{}.__init__'.format(name)
        if  package_name in self.memory:
            return package_name
        return False

    __contains__ = contains  # Convenience.

    def find_module(self, fullname, path=None):
        """Find the module in the dict."""
        if fullname in self:
            return self
        return None

    def source_path(self, fullname):
        """Return the module name if the module is in the dict."""
        if not fullname in self:
            raise ImportError
        return fullname

    def get_data(self, path):
        """Return the bytes for the source.

        The value found in the dict is passed through 'bytes' before being
        returned.

        """
        name = self.contains(path)
        if not name:
            raise IOError
        return bytes(self.memory[name])

    def is_package(self, fullname):
        """Tell if module is a package based on whether the dict contains the
        name with ``.__init__`` appended to it."""
        if fullname not in self:
            raise ImportError
        if fullname in self.memory:
            return False
        # If name is in this importer but not as it is then it must end in
        # ``__init__``.
        else:
            return True</pre>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../contents.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">29.6. <tt class="docutils literal"><span class="pre">importlib</span></tt> &#8211; An implementation of <tt class="docutils literal"><span class="pre">import</span></tt></a><ul>
<li><a class="reference external" href="#introduction">29.6.1. Introduction</a></li>
<li><a class="reference external" href="#functions">29.6.2. Functions</a></li>
<li><a class="reference external" href="#module-importlib.abc">29.6.3. <tt class="docutils literal"><span class="pre">importlib.abc</span></tt> &#8211; Abstract base classes related to import</a></li>
<li><a class="reference external" href="#module-importlib.machinery">29.6.4. <tt class="docutils literal"><span class="pre">importlib.machinery</span></tt> &#8211; Importers and path hooks</a></li>
<li><a class="reference external" href="#module-importlib.util">29.6.5. <tt class="docutils literal"><span class="pre">importlib.util</span></tt> &#8211; Utility code for importers</a></li>
<li><a class="reference external" href="#example">29.6.6. Example</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="runpy.html"
                                  title="previous chapter">29.5. <tt class="docutils literal"><span class="pre">runpy</span></tt> &#8212; Locating and executing Python modules</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="language.html"
                                  title="next chapter">30. Python Language Services</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/library/importlib.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="language.html" title="30. Python Language Services"
             >next</a> |</li>
        <li class="right" >
          <a href="runpy.html" title="29.5. runpy — Locating and executing Python modules"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v3.1.1 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="modules.html" >29. Importing Modules</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2009, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.  
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Aug 16, 2009.
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.2.
    </div>

  </body>
</html>