korsygfhrtzangaiide
Elepffwdsff
/
usr
/
share
/
doc
/
python-docs-2.7.5
/
html
/
library
/
Upload FileeE
HOME
<!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>30.4. zipimport — Import modules from Zip archives — Python 2.7.5 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: '2.7.5', 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 2.7.5 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 2.7.5 documentation" href="../index.html" /> <link rel="up" title="30. Importing Modules" href="modules.html" /> <link rel="next" title="30.5. pkgutil — Package extension utility" href="pkgutil.html" /> <link rel="prev" title="30.3. imputil — Import utilities" href="imputil.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </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="30.5. pkgutil — Package extension utility" accesskey="N">next</a> |</li> <li class="right" > <a href="imputil.html" title="30.3. imputil — Import utilities" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="modules.html" accesskey="U">30. Importing Modules</a> »</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>30.4. <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> — Import modules from Zip archives<a class="headerlink" href="#module-zipimport" title="Permalink to this headline">¶</a></h1> <p class="versionadded"> <span class="versionmodified">New in version 2.3.</span></p> <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">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’t contain <tt class="file docutils literal"><span class="pre">.pyc</span></tt> files, importing may be rather slow.</p> <p>Using the built-in <a class="reference internal" href="functions.html#reload" title="reload"><tt class="xref py py-func docutils literal"><span class="pre">reload()</span></tt></a> function will fail if called on a module loaded from a ZIP archive; it is unlikely that <a class="reference internal" href="functions.html#reload" title="reload"><tt class="xref py py-func docutils literal"><span class="pre">reload()</span></tt></a> would be needed, since this would imply that the ZIP has been altered during runtime.</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’s a subclass of <a class="reference internal" href="exceptions.html#exceptions.ImportError" title="exceptions.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#exceptions.ImportError" title="exceptions.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>30.4.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’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 docutils literal"><span class="pre">None</span></tt></a> if it wasn’t. The optional <em>path</em> argument is ignored—it’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’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#exceptions.IOError" title="exceptions.IOError"><tt class="xref py py-exc docutils literal"><span class="pre">IOError</span></tt></a> if the file wasn’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’t be found.</p> </dd></dl> <p class="versionadded"> <span class="versionmodified">New in version 2.7.</span></p> <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’t be found, return <a class="reference internal" href="constants.html#None" title="None"><tt class="xref py py-const 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’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’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’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>30.4.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-python"><pre>$ unzip -l example.zip Archive: 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) >>> import sys >>> sys.path.insert(0, 'example.zip') # Add .zip file to front of path >>> import jwzthreading >>> jwzthreading.__file__ '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="#">30.4. <tt class="docutils literal"><span class="pre">zipimport</span></tt> — Import modules from Zip archives</a><ul> <li><a class="reference internal" href="#zipimporter-objects">30.4.1. zipimporter Objects</a></li> <li><a class="reference internal" href="#examples">30.4.2. Examples</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="imputil.html" title="previous chapter">30.3. <tt class="docutils literal"><span class="pre">imputil</span></tt> — Import utilities</a></p> <h4>Next topic</h4> <p class="topless"><a href="pkgutil.html" title="next chapter">30.5. <tt class="docutils literal"><span class="pre">pkgutil</span></tt> — 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" /> <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="30.5. pkgutil — Package extension utility" >next</a> |</li> <li class="right" > <a href="imputil.html" title="30.3. imputil — Import utilities" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="modules.html" >30. Importing Modules</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2019, 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 Jul 03, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>