Kerala Cyber
Warriors
KCW Uploader V1.1
<!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="X-UA-Compatible" content="IE=Edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>31.4. zipimport — Import modules from Zip archives — Python 2.7.18 documentation</title>
<link rel="stylesheet" href="../_static/classic.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></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/language_data.js"></script>
<script type="text/javascript" src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="Search within Python 2.7.18 documentation"
href="../_static/opensearch.xml"/>
<link rel="author" title="About these documents" href="../about.html" />
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="copyright" title="Copyright" href="../copyright.html" />
<link rel="next" title="31.5. pkgutil — Package extension utility" href="pkgutil.html" />
<link rel="prev" title="31.3. imputil — Import utilities" href="imputil.html" />
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
<link rel="canonical" href="file:///usr/share/doc/python2.7/html/library/zipimport.html" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
</head><body>
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
This document is for an old version of Python that is <a href="https://devguide.python.org/devcycle/#end-of-life-branches">no longer supported</a>.
You should install the python3 and python3-doc packages and read the
<a href="file:///usr/share/doc/python3-doc/html/library/zipimport.html"> Python documentation for the Python3 version packaged in this release</a>.
</div>
<div class="related" role="navigation" aria-label="related navigation">
<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="31.5. pkgutil — Package extension utility"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="imputil.html" title="31.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="https://www.python.org/">Python</a> »</li>
<li>
<a href="../index.html">Python 2.7.18 documentation</a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
<li class="nav-item nav-item-2"><a href="modules.html" accesskey="U">31. Importing Modules</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="module-zipimport">
<span id="zipimport-import-modules-from-zip-archives"></span><h1>31.4. <a class="reference internal" href="#module-zipimport" title="zipimport: support for importing Python modules from ZIP archives."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipimport</span></code></a> — Import modules from Zip archives<a class="headerlink" href="#module-zipimport" title="Permalink to this headline">¶</a></h1>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.3.</span></p>
</div>
<p>This module adds the ability to import Python modules (<code class="file docutils literal notranslate"><span class="pre">*.py</span></code>,
<code class="file docutils literal notranslate"><span class="pre">*.py[co]</span></code>) 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."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipimport</span></code></a> module explicitly; it is automatically used
by the built-in <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> mechanism for <a class="reference internal" href="sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> items that are paths
to ZIP archives.</p>
<p>Typically, <a class="reference internal" href="sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></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"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></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 <code class="file docutils literal notranslate"><span class="pre">example.zip/lib/</span></code> would only
import from the <code class="file docutils literal notranslate"><span class="pre">lib/</span></code> subdirectory within the archive.</p>
<p>Any files may be present in the ZIP archive, but only files <code class="file docutils literal notranslate"><span class="pre">.py</span></code> and
<code class="file docutils literal notranslate"><span class="pre">.py[co]</span></code> are available for import. ZIP import of dynamic modules
(<code class="file docutils literal notranslate"><span class="pre">.pyd</span></code>, <code class="file docutils literal notranslate"><span class="pre">.so</span></code>) is disallowed. Note that if an archive only contains
<code class="file docutils literal notranslate"><span class="pre">.py</span></code> files, Python will not attempt to modify the archive by adding the
corresponding <code class="file docutils literal notranslate"><span class="pre">.pyc</span></code> or <code class="file docutils literal notranslate"><span class="pre">.pyo</span></code> file, meaning that if a ZIP archive
doesn’t contain <code class="file docutils literal notranslate"><span class="pre">.pyc</span></code> files, importing may be rather slow.</p>
<p>Using the built-in <a class="reference internal" href="functions.html#reload" title="reload"><code class="xref py py-func docutils literal notranslate"><span class="pre">reload()</span></code></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"><code class="xref py py-func docutils literal notranslate"><span class="pre">reload()</span></code></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 seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><a class="reference external" href="https://pkware.cachefly.net/webdocs/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="https://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="https://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><code class="descclassname">zipimport.</code><code class="descname">ZipImportError</code><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"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a>,
so it can be caught as <a class="reference internal" href="exceptions.html#exceptions.ImportError" title="exceptions.ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a>, too.</p>
</dd></dl>
<div class="section" id="zipimporter-objects">
<span id="id1"></span><h2>31.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"><code class="xref py py-class docutils literal notranslate"><span class="pre">zipimporter</span></code></a> is the class for importing ZIP files.</p>
<dl class="class">
<dt id="zipimport.zipimporter">
<em class="property">class </em><code class="descclassname">zipimport.</code><code class="descname">zipimporter</code><span class="sig-paren">(</span><em>archivepath</em><span class="sig-paren">)</span><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 <code class="file docutils literal notranslate"><span class="pre">foo/bar.zip/lib</span></code> will look for modules in the <code class="file docutils literal notranslate"><span class="pre">lib</span></code> directory
inside the ZIP file <code class="file docutils literal notranslate"><span class="pre">foo/bar.zip</span></code> (provided that it exists).</p>
<p><a class="reference internal" href="#zipimport.ZipImportError" title="zipimport.ZipImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ZipImportError</span></code></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">
<code class="descname">find_module</code><span class="sig-paren">(</span><em>fullname</em><span class="optional">[</span>, <em>path</em><span class="optional">]</span><span class="sig-paren">)</span><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"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></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">
<code class="descname">get_code</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><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"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ZipImportError</span></code></a> if the module couldn’t be found.</p>
</dd></dl>
<dl class="method">
<dt id="zipimport.zipimporter.get_data">
<code class="descname">get_data</code><span class="sig-paren">(</span><em>pathname</em><span class="sig-paren">)</span><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"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IOError</span></code></a> if the
file wasn’t found.</p>
</dd></dl>
<dl class="method">
<dt id="zipimport.zipimporter.get_filename">
<code class="descname">get_filename</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#zipimport.zipimporter.get_filename" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the value <code class="docutils literal notranslate"><span class="pre">__file__</span></code> would be set to if the specified module
was imported. Raise <a class="reference internal" href="#zipimport.ZipImportError" title="zipimport.ZipImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ZipImportError</span></code></a> if the module couldn’t be
found.</p>
</dd></dl>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.7.</span></p>
</div>
<dl class="method">
<dt id="zipimport.zipimporter.get_source">
<code class="descname">get_source</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><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"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ZipImportError</span></code></a> if the module couldn’t be found, return
<a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></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">
<code class="descname">is_package</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#zipimport.zipimporter.is_package" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the module specified by <em>fullname</em> is a package. Raise
<a class="reference internal" href="#zipimport.ZipImportError" title="zipimport.ZipImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ZipImportError</span></code></a> if the module couldn’t be found.</p>
</dd></dl>
<dl class="method">
<dt id="zipimport.zipimporter.load_module">
<code class="descname">load_module</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><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"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ZipImportError</span></code></a> if it wasn’t found.</p>
</dd></dl>
<dl class="attribute">
<dt id="zipimport.zipimporter.archive">
<code class="descname">archive</code><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">
<code class="descname">prefix</code><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"><code class="xref py py-attr docutils literal notranslate"><span class="pre">archive</span></code></a> and <a class="reference internal" href="#zipimport.zipimporter.prefix" title="zipimport.zipimporter.prefix"><code class="xref py py-attr docutils literal notranslate"><span class="pre">prefix</span></code></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"><code class="xref py py-class docutils literal notranslate"><span class="pre">zipimporter</span></code></a> constructor.</p>
</dd></dl>
</div>
<div class="section" id="examples">
<span id="zipimport-examples"></span><h2>31.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."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipimport</span></code></a> module is not explicitly used.</p>
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> unzip -l example.zip
<span class="go">Archive: example.zip</span>
<span class="go"> Length Date Time Name</span>
<span class="go"> -------- ---- ---- ----</span>
<span class="go"> 8467 11-26-02 22:30 jwzthreading.py</span>
<span class="go"> -------- -------</span>
<span class="go"> 8467 1 file</span>
<span class="gp">$</span> ./python
<span class="go">Python 2.3 (#1, Aug 1 2003, 19:54:32)</span>
<span class="gp">></span>>> import sys
<span class="gp">></span>>> sys.path.insert<span class="o">(</span><span class="m">0</span>, <span class="s1">'example.zip'</span><span class="o">)</span> <span class="c1"># Add .zip file to front of path</span>
<span class="gp">></span>>> import jwzthreading
<span class="gp">></span>>> jwzthreading.__file__
<span class="go">'example.zip/jwzthreading.py'</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../contents.html">Table of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">31.4. <code class="docutils literal notranslate"><span class="pre">zipimport</span></code> — Import modules from Zip archives</a><ul>
<li><a class="reference internal" href="#zipimporter-objects">31.4.1. zipimporter Objects</a></li>
<li><a class="reference internal" href="#examples">31.4.2. Examples</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="imputil.html"
title="previous chapter">31.3. <code class="docutils literal notranslate"><span class="pre">imputil</span></code> — Import utilities</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="pkgutil.html"
title="next chapter">31.5. <code class="docutils literal notranslate"><span class="pre">pkgutil</span></code> — Package extension utility</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/library/zipimport.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<div class="searchformwrapper">
<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>
</div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<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="31.5. pkgutil — Package extension utility"
>next</a> |</li>
<li class="right" >
<a href="imputil.html" title="31.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="https://www.python.org/">Python</a> »</li>
<li>
<a href="../index.html">Python 2.7.18 documentation</a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
<li class="nav-item nav-item-2"><a href="modules.html" >31. Importing Modules</a> »</li>
</ul>
</div>
<div class="footer">
© <a href="../copyright.html">Copyright</a> 1990-2024, Python Software Foundation.
<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
Last updated on November 21, 2024.
<a href="../bugs.html">Found a bug</a>?
<br />
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.8.5.
</div>
</body>
</html>
-=[ KCW uplo4d3r c0ded by cJ_n4p573r ]=-
Ⓒ2017 ҠЄГѦLѦ СүѣЄГ ЩѦГГіѺГՏ