Sophie

Sophie

distrib > Mandriva > 2010.1 > x86_64 > media > contrib-backports > by-pkgid > 3ba3bd1608c672ba2129b098a48e9e4d > files > 866

python3-docs-3.2.2-3mdv2010.2.noarch.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.2. zipimport — Import modules from Zip archives &mdash; Python v3.2.2 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.2.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v3.2.2 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.2.2 documentation" href="../index.html" />
    <link rel="up" title="29. Importing Modules" href="modules.html" />
    <link rel="next" title="29.3. pkgutil — Package extension utility" href="pkgutil.html" />
    <link rel="prev" title="29.1. imp — Access the import internals" href="imp.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="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="pkgutil.html" title="29.3. pkgutil — Package extension utility"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="imp.html" title="29.1. imp — Access the import internals"
             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.2.2 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-zipimport">
<span id="zipimport-import-modules-from-zip-archives"></span><h1>29.2. <a class="reference internal" href="#module-zipimport" title="zipimport: support for importing Python modules from ZIP archives."><tt class="xref py py-mod docutils literal"><span class="pre">zipimport</span></tt></a> &#8212; Import modules from Zip archives<a class="headerlink" href="#module-zipimport" title="Permalink to this headline">¶</a></h1>
<p>This module adds the ability to import Python modules (<tt class="file docutils literal"><span class="pre">*.py</span></tt>,
<tt class="file docutils literal"><span class="pre">*.py[co]</span></tt>) and packages from ZIP-format archives. It is usually not
needed to use the <a class="reference internal" href="#module-zipimport" title="zipimport: support for importing Python modules from ZIP archives."><tt class="xref py py-mod docutils literal"><span class="pre">zipimport</span></tt></a> module explicitly; it is automatically used
by the built-in <a class="reference internal" href="../reference/simple_stmts.html#import"><tt class="xref std std-keyword docutils literal"><span class="pre">import</span></tt></a> mechanism for <a class="reference internal" href="sys.html#sys.path" title="sys.path"><tt class="xref py py-data docutils literal"><span class="pre">sys.path</span></tt></a> items that are paths
to ZIP archives.</p>
<p>Typically, <a class="reference internal" href="sys.html#sys.path" title="sys.path"><tt class="xref py py-data docutils literal"><span class="pre">sys.path</span></tt></a> is a list of directory names as strings.  This module
also allows an item of <a class="reference internal" href="sys.html#sys.path" title="sys.path"><tt class="xref py py-data docutils literal"><span class="pre">sys.path</span></tt></a> to be a string naming a ZIP file archive.
The ZIP archive can contain a subdirectory structure to support package imports,
and a path within the archive can be specified to only import from a
subdirectory.  For example, the path <tt class="file docutils literal"><span class="pre">/tmp/example.zip/lib/</span></tt> would only
import from the <tt class="file docutils literal"><span class="pre">lib/</span></tt> subdirectory within the archive.</p>
<p>Any files may be present in the ZIP archive, but only files <tt class="file docutils literal"><span class="pre">.py</span></tt> and
<tt class="file docutils literal"><span class="pre">.py[co]</span></tt> are available for import.  ZIP import of dynamic modules
(<tt class="file docutils literal"><span class="pre">.pyd</span></tt>, <tt class="file docutils literal"><span class="pre">.so</span></tt>) is disallowed. Note that if an archive only contains
<tt class="file docutils literal"><span class="pre">.py</span></tt> files, Python will not attempt to modify the archive by adding the
corresponding <tt class="file docutils literal"><span class="pre">.pyc</span></tt> or <tt class="file docutils literal"><span class="pre">.pyo</span></tt> file, meaning that if a ZIP archive
doesn&#8217;t contain <tt class="file docutils literal"><span class="pre">.pyc</span></tt> files, importing may be rather slow.</p>
<p>ZIP archives with an archive comment are currently not supported.</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="http://www.pkware.com/documents/casestudies/APPNOTE.TXT">PKZIP Application Note</a></dt>
<dd>Documentation on the ZIP file format by Phil Katz, the creator of the format and
algorithms used.</dd>
<dt><span class="target" id="index-0"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0273"><strong>PEP 273</strong></a> - Import Modules from Zip Archives</dt>
<dd>Written by James C. Ahlstrom, who also provided an implementation. Python 2.3
follows the specification in PEP 273, but uses an implementation written by Just
van Rossum that uses the import hooks described in PEP 302.</dd>
<dt><span class="target" id="index-1"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> - New Import Hooks</dt>
<dd>The PEP to add the import hooks that help this module work.</dd>
</dl>
</div>
<p>This module defines an exception:</p>
<dl class="exception">
<dt id="zipimport.ZipImportError">
<em class="property">exception </em><tt class="descclassname">zipimport.</tt><tt class="descname">ZipImportError</tt><a class="headerlink" href="#zipimport.ZipImportError" title="Permalink to this definition">¶</a></dt>
<dd><p>Exception raised by zipimporter objects. It&#8217;s a subclass of <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><tt class="xref py py-exc docutils literal"><span class="pre">ImportError</span></tt></a>,
so it can be caught as <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><tt class="xref py py-exc docutils literal"><span class="pre">ImportError</span></tt></a>, too.</p>
</dd></dl>

<div class="section" id="zipimporter-objects">
<span id="id1"></span><h2>29.2.1. zipimporter Objects<a class="headerlink" href="#zipimporter-objects" title="Permalink to this headline">¶</a></h2>
<p><a class="reference internal" href="#zipimport.zipimporter" title="zipimport.zipimporter"><tt class="xref py py-class docutils literal"><span class="pre">zipimporter</span></tt></a> is the class for importing ZIP files.</p>
<dl class="class">
<dt id="zipimport.zipimporter">
<em class="property">class </em><tt class="descclassname">zipimport.</tt><tt class="descname">zipimporter</tt><big>(</big><em>archivepath</em><big>)</big><a class="headerlink" href="#zipimport.zipimporter" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a new zipimporter instance. <em>archivepath</em> must be a path to a ZIP
file, or to a specific path within a ZIP file.  For example, an <em>archivepath</em>
of <tt class="file docutils literal"><span class="pre">foo/bar.zip/lib</span></tt> will look for modules in the <tt class="file docutils literal"><span class="pre">lib</span></tt> directory
inside the ZIP file <tt class="file docutils literal"><span class="pre">foo/bar.zip</span></tt> (provided that it exists).</p>
<p><a class="reference internal" href="#zipimport.ZipImportError" title="zipimport.ZipImportError"><tt class="xref py py-exc docutils literal"><span class="pre">ZipImportError</span></tt></a> is raised if <em>archivepath</em> doesn&#8217;t point to a valid ZIP
archive.</p>
<dl class="method">
<dt id="zipimport.zipimporter.find_module">
<tt class="descname">find_module</tt><big>(</big><em>fullname</em><span class="optional">[</span>, <em>path</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#zipimport.zipimporter.find_module" title="Permalink to this definition">¶</a></dt>
<dd><p>Search for a module specified by <em>fullname</em>. <em>fullname</em> must be the fully
qualified (dotted) module name. It returns the zipimporter instance itself
if the module was found, or <a class="reference internal" href="constants.html#None" title="None"><tt class="xref py py-const xref docutils literal"><span class="pre">None</span></tt></a> if it wasn&#8217;t. The optional
<em>path</em> argument is ignored&#8212;it&#8217;s there for compatibility with the
importer protocol.</p>
</dd></dl>

<dl class="method">
<dt id="zipimport.zipimporter.get_code">
<tt class="descname">get_code</tt><big>(</big><em>fullname</em><big>)</big><a class="headerlink" href="#zipimport.zipimporter.get_code" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the code object for the specified module. Raise
<a class="reference internal" href="#zipimport.ZipImportError" title="zipimport.ZipImportError"><tt class="xref py py-exc docutils literal"><span class="pre">ZipImportError</span></tt></a> if the module couldn&#8217;t be found.</p>
</dd></dl>

<dl class="method">
<dt id="zipimport.zipimporter.get_data">
<tt class="descname">get_data</tt><big>(</big><em>pathname</em><big>)</big><a class="headerlink" href="#zipimport.zipimporter.get_data" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the data associated with <em>pathname</em>. Raise <a class="reference internal" href="exceptions.html#IOError" title="IOError"><tt class="xref py py-exc docutils literal"><span class="pre">IOError</span></tt></a> if the
file wasn&#8217;t found.</p>
</dd></dl>

<dl class="method">
<dt id="zipimport.zipimporter.get_filename">
<tt class="descname">get_filename</tt><big>(</big><em>fullname</em><big>)</big><a class="headerlink" href="#zipimport.zipimporter.get_filename" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the value <tt class="docutils literal"><span class="pre">__file__</span></tt> would be set to if the specified module
was imported. Raise <a class="reference internal" href="#zipimport.ZipImportError" title="zipimport.ZipImportError"><tt class="xref py py-exc docutils literal"><span class="pre">ZipImportError</span></tt></a> if the module couldn&#8217;t be
found.</p>
<p class="versionadded">
<span class="versionmodified">New in version 3.1.</span></p>
</dd></dl>

<dl class="method">
<dt id="zipimport.zipimporter.get_source">
<tt class="descname">get_source</tt><big>(</big><em>fullname</em><big>)</big><a class="headerlink" href="#zipimport.zipimporter.get_source" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the source code for the specified module. Raise
<a class="reference internal" href="#zipimport.ZipImportError" title="zipimport.ZipImportError"><tt class="xref py py-exc docutils literal"><span class="pre">ZipImportError</span></tt></a> if the module couldn&#8217;t be found, return
<a class="reference internal" href="constants.html#None" title="None"><tt class="xref py py-const xref docutils literal"><span class="pre">None</span></tt></a> if the archive does contain the module, but has no source
for it.</p>
</dd></dl>

<dl class="method">
<dt id="zipimport.zipimporter.is_package">
<tt class="descname">is_package</tt><big>(</big><em>fullname</em><big>)</big><a class="headerlink" href="#zipimport.zipimporter.is_package" title="Permalink to this definition">¶</a></dt>
<dd><p>Return True if the module specified by <em>fullname</em> is a package. Raise
<a class="reference internal" href="#zipimport.ZipImportError" title="zipimport.ZipImportError"><tt class="xref py py-exc docutils literal"><span class="pre">ZipImportError</span></tt></a> if the module couldn&#8217;t be found.</p>
</dd></dl>

<dl class="method">
<dt id="zipimport.zipimporter.load_module">
<tt class="descname">load_module</tt><big>(</big><em>fullname</em><big>)</big><a class="headerlink" href="#zipimport.zipimporter.load_module" title="Permalink to this definition">¶</a></dt>
<dd><p>Load the module specified by <em>fullname</em>. <em>fullname</em> must be the fully
qualified (dotted) module name. It returns the imported module, or raises
<a class="reference internal" href="#zipimport.ZipImportError" title="zipimport.ZipImportError"><tt class="xref py py-exc docutils literal"><span class="pre">ZipImportError</span></tt></a> if it wasn&#8217;t found.</p>
</dd></dl>

<dl class="attribute">
<dt id="zipimport.zipimporter.archive">
<tt class="descname">archive</tt><a class="headerlink" href="#zipimport.zipimporter.archive" title="Permalink to this definition">¶</a></dt>
<dd><p>The file name of the importer&#8217;s associated ZIP file, without a possible
subpath.</p>
</dd></dl>

<dl class="attribute">
<dt id="zipimport.zipimporter.prefix">
<tt class="descname">prefix</tt><a class="headerlink" href="#zipimport.zipimporter.prefix" title="Permalink to this definition">¶</a></dt>
<dd><p>The subpath within the ZIP file where modules are searched.  This is the
empty string for zipimporter objects which point to the root of the ZIP
file.</p>
</dd></dl>

<p>The <a class="reference internal" href="#zipimport.zipimporter.archive" title="zipimport.zipimporter.archive"><tt class="xref py py-attr docutils literal"><span class="pre">archive</span></tt></a> and <a class="reference internal" href="#zipimport.zipimporter.prefix" title="zipimport.zipimporter.prefix"><tt class="xref py py-attr docutils literal"><span class="pre">prefix</span></tt></a> attributes, when combined with a
slash, equal the original <em>archivepath</em> argument given to the
<a class="reference internal" href="#zipimport.zipimporter" title="zipimport.zipimporter"><tt class="xref py py-class docutils literal"><span class="pre">zipimporter</span></tt></a> constructor.</p>
</dd></dl>

</div>
<div class="section" id="examples">
<span id="zipimport-examples"></span><h2>29.2.2. Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h2>
<p>Here is an example that imports a module from a ZIP archive - note that the
<a class="reference internal" href="#module-zipimport" title="zipimport: support for importing Python modules from ZIP archives."><tt class="xref py py-mod docutils literal"><span class="pre">zipimport</span></tt></a> module is not explicitly used.</p>
<div class="highlight-python3"><pre>$ unzip -l /tmp/example.zip
Archive:  /tmp/example.zip
  Length     Date   Time    Name
 --------    ----   ----    ----
     8467  11-26-02 22:30   jwzthreading.py
 --------                   -------
     8467                   1 file
$ ./python
Python 2.3 (#1, Aug 1 2003, 19:54:32)
&gt;&gt;&gt; import sys
&gt;&gt;&gt; sys.path.insert(0, '/tmp/example.zip')  # Add .zip file to front of path
&gt;&gt;&gt; import jwzthreading
&gt;&gt;&gt; jwzthreading.__file__
'/tmp/example.zip/jwzthreading.py'</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 internal" href="#">29.2. <tt class="docutils literal"><span class="pre">zipimport</span></tt> &#8212; Import modules from Zip archives</a><ul>
<li><a class="reference internal" href="#zipimporter-objects">29.2.1. zipimporter Objects</a></li>
<li><a class="reference internal" href="#examples">29.2.2. Examples</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="imp.html"
                        title="previous chapter">29.1. <tt class="docutils literal docutils literal docutils literal"><span class="pre">imp</span></tt> &#8212; Access the <tt class="docutils literal docutils literal docutils literal"><span class="pre">import</span></tt> internals</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="pkgutil.html"
                        title="next chapter">29.3. <tt class="docutils literal docutils literal docutils literal"><span class="pre">pkgutil</span></tt> &#8212; Package extension utility</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/library/zipimport.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="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="pkgutil.html" title="29.3. pkgutil — Package extension utility"
             >next</a> |</li>
        <li class="right" >
          <a href="imp.html" title="29.1. imp — Access the import internals"
             >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.2.2 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-2011, 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 Sep 04, 2011.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
    </div>

  </body>
</html>