349 lines
23 KiB
HTML
349 lines
23 KiB
HTML
|
||
<!DOCTYPE html>
|
||
|
||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<title>zipimport — Import modules from Zip archives — Python 3.7.4 documentation</title>
|
||
<link rel="stylesheet" href="../_static/pydoctheme.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 3.7.4 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="pkgutil — Package extension utility" href="pkgutil.html" />
|
||
<link rel="prev" title="Importing Modules" href="modules.html" />
|
||
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
|
||
<link rel="canonical" href="https://docs.python.org/3/library/zipimport.html" />
|
||
|
||
<script type="text/javascript" src="../_static/copybutton.js"></script>
|
||
<script type="text/javascript" src="../_static/switchers.js"></script>
|
||
|
||
|
||
|
||
<style>
|
||
@media only screen {
|
||
table.full-width-table {
|
||
width: 100%;
|
||
}
|
||
}
|
||
</style>
|
||
|
||
|
||
</head><body>
|
||
|
||
<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="pkgutil — Package extension utility"
|
||
accesskey="N">next</a> |</li>
|
||
<li class="right" >
|
||
<a href="modules.html" title="Importing Modules"
|
||
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>
|
||
<span class="language_switcher_placeholder">en</span>
|
||
<span class="version_switcher_placeholder">3.7.4</span>
|
||
<a href="../index.html">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">Importing Modules</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" style="display: none" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="Quick search" 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>
|
||
<script type="text/javascript">$('.inline-search').show(0);</script>
|
||
|
|
||
</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><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>
|
||
<hr class="docutils" />
|
||
<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">*.pyc</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">.pyc</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> 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>ZIP archives with an archive comment are currently not supported.</p>
|
||
<div class="admonition seealso">
|
||
<p class="admonition-title">See also</p>
|
||
<dl class="simple">
|
||
<dt><a class="reference external" href="https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT">PKZIP Application Note</a></dt><dd><p>Documentation on the ZIP file format by Phil Katz, the creator of the format and
|
||
algorithms used.</p>
|
||
</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><p>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.</p>
|
||
</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><p>The PEP to add the import hooks that help this module work.</p>
|
||
</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#ImportError" title="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#ImportError" title="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>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#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> if the
|
||
file wasn’t found.</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.3: </span><a class="reference internal" href="exceptions.html#IOError" title="IOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IOError</span></code></a> used to be raised instead of <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>.</p>
|
||
</div>
|
||
</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>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">New in version 3.1.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<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>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="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipimport</span></code> — Import modules from Zip archives</a><ul>
|
||
<li><a class="reference internal" href="#zipimporter-objects">zipimporter Objects</a></li>
|
||
<li><a class="reference internal" href="#examples">Examples</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>Previous topic</h4>
|
||
<p class="topless"><a href="modules.html"
|
||
title="previous chapter">Importing Modules</a></p>
|
||
<h4>Next topic</h4>
|
||
<p class="topless"><a href="pkgutil.html"
|
||
title="next chapter"><code class="xref py py-mod 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="../bugs.html">Report a Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.7/Doc/library/zipimport.rst"
|
||
rel="nofollow">Show Source
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</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="pkgutil — Package extension utility"
|
||
>next</a> |</li>
|
||
<li class="right" >
|
||
<a href="modules.html" title="Importing Modules"
|
||
>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>
|
||
<span class="language_switcher_placeholder">en</span>
|
||
<span class="version_switcher_placeholder">3.7.4</span>
|
||
<a href="../index.html">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" >Importing Modules</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" style="display: none" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="Quick search" 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>
|
||
<script type="text/javascript">$('.inline-search').show(0);</script>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
<div class="footer">
|
||
© <a href="../copyright.html">Copyright</a> 2001-2019, 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 Jul 13, 2019.
|
||
<a href="../bugs.html">Found a bug</a>?
|
||
<br />
|
||
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.0.1.
|
||
</div>
|
||
|
||
</body>
|
||
</html> |