Team Ai
Apppublic

parthtamu/rag-code-assistant

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
zipimport.html559 linesDownload Raw Back to docs
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4  <head>5    <meta charset="utf-8" />6    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="zipimport — Import modules from Zip archives" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/zipimport.html" />10<meta property="og:site_name" content="Python documentation" />11<meta property="og:description" content="Source code: Lib/zipimport.py This module adds the ability to import Python modules (*.py,*.pyc) and packages from ZIP-format archives. It is usually not needed to use the zipimport module explicit..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_zipimport_aeea7080.png" />15<meta property="og:image:alt" content="Source code: Lib/zipimport.py This module adds the ability to import Python modules (*.py,*.pyc) and packages from ZIP-format archives. It is usually not needed to use the zipimport module explicit..." />16<meta name="description" content="Source code: Lib/zipimport.py This module adds the ability to import Python modules (*.py,*.pyc) and packages from ZIP-format archives. It is usually not needed to use the zipimport module explicit..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20    <title>zipimport — Import modules from Zip archives &#8212; Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21    22    <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23    <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24    <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25    <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26    <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27    28    <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29    <script src="../_static/doctools.js?v=9bcbadda"></script>30    <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31    <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32    33    <script src="../_static/sidebar.js"></script>34    35    <link rel="search" type="application/opensearchdescription+xml"36          title="Search within Python 3.15.0a6 documentation"37          href="../_static/opensearch.xml"/>38    <link rel="author" title="About these documents" href="../about.html" />39    <link rel="index" title="Index" href="../genindex.html" />40    <link rel="search" title="Search" href="../search.html" />41    <link rel="copyright" title="Copyright" href="../copyright.html" />42    <link rel="next" title="pkgutil — Package extension utility" href="pkgutil.html" />43    <link rel="prev" title="Importing Modules" href="modules.html" />44    45      46      <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47      48      <link rel="canonical" href="https://docs.python.org/3/library/zipimport.html">49      50    51 52    53    <style>54      @media only screen {55        table.full-width-table {56            width: 100%;57        }58      }59    </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61    <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62            <script type="text/javascript" src="../_static/copybutton.js"></script>63            <script type="text/javascript" src="../_static/menu.js"></script>64            <script type="text/javascript" src="../_static/search-focus.js"></script>65            <script type="text/javascript" src="../_static/themetoggle.js"></script> 66            <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67            <meta name="readthedocs-addons-api-version" content="1">68 69  </head>70<body>71<div class="mobile-nav">72    <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73           aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74    <nav class="nav-content" role="navigation">75        <label for="menuToggler" class="toggler__label">76            <span></span>77        </label>78        <span class="nav-items-wrapper">79            <a href="https://www.python.org/" class="nav-logo">80                <img src="../_static/py.svg" alt="Python logo">81            </a>82            <span class="version_switcher_placeholder"></span>83            <form role="search" class="search" action="../search.html" method="get">84                <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85                    <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>86                </svg>87                <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88                <input type="submit" value="Go">89            </form>90        </span>91    </nav>92    <div class="menu-wrapper">93        <nav class="menu" role="navigation" aria-label="main navigation">94            <div class="language_switcher_placeholder"></div>95            96<label class="theme-selector-label">97    Theme98    <select class="theme-selector" oninput="activateTheme(this.value)">99        <option value="auto" selected>Auto</option>100        <option value="light">Light</option>101        <option value="dark">Dark</option>102    </select>103</label>104  <div>105    <h3><a href="../contents.html">Table of Contents</a></h3>106    <ul>107<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>108<li><a class="reference internal" href="#zipimporter-objects">zipimporter Objects</a></li>109<li><a class="reference internal" href="#examples">Examples</a></li>110</ul>111</li>112</ul>113 114  </div>115  <div>116    <h4>Previous topic</h4>117    <p class="topless"><a href="modules.html"118                          title="previous chapter">Importing Modules</a></p>119  </div>120  <div>121    <h4>Next topic</h4>122    <p class="topless"><a href="pkgutil.html"123                          title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pkgutil</span></code> — Package extension utility</a></p>124  </div>125  <script>126    document.addEventListener('DOMContentLoaded', () => {127        const title = document.querySelector('meta[property="og:title"]').content;128        const elements = document.querySelectorAll('.improvepage');129        const pageurl = window.location.href.split('?')[0];130        elements.forEach(element => {131            const url = new URL(element.href.split('?')[0].replace("-nojs", ""));132            url.searchParams.set('pagetitle', title);133            url.searchParams.set('pageurl', pageurl);134            url.searchParams.set('pagesource', "library/zipimport.rst");135            element.href = url.toString();136        });137    });138  </script>139  <div role="note" aria-label="source link">140    <h3>This page</h3>141    <ul class="this-page-menu">142      <li><a href="../bugs.html">Report a bug</a></li>143      <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>144      <li>145        <a href="https://github.com/python/cpython/blob/main/Doc/library/zipimport.rst?plain=1"146            rel="nofollow">Show source147        </a>148      </li>149      150    </ul>151  </div>152        </nav>153    </div>154</div>155 156  157    <div class="related" role="navigation" aria-label="Related">158      <h3>Navigation</h3>159      <ul>160        <li class="right" style="margin-right: 10px">161          <a href="../genindex.html" title="General Index"162             accesskey="I">index</a></li>163        <li class="right" >164          <a href="../py-modindex.html" title="Python Module Index"165             >modules</a> |</li>166        <li class="right" >167          <a href="pkgutil.html" title="pkgutil — Package extension utility"168             accesskey="N">next</a> |</li>169        <li class="right" >170          <a href="modules.html" title="Importing Modules"171             accesskey="P">previous</a> |</li>172 173          <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>174          <li><a href="https://www.python.org/">Python</a> &#187;</li>175          <li class="switchers">176            <div class="language_switcher_placeholder"></div>177            <div class="version_switcher_placeholder"></div>178          </li>179          <li>180              181          </li>182    <li id="cpython-language-and-version">183      <a href="../index.html">3.15.0a6 Documentation</a> &#187;184    </li>185 186          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>187          <li class="nav-item nav-item-2"><a href="modules.html" accesskey="U">Importing Modules</a> &#187;</li>188        <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipimport</span></code> — Import modules from Zip archives</a></li>189                <li class="right">190                    191 192    <div class="inline-search" role="search">193        <form class="inline-search" action="../search.html" method="get">194          <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">195          <input type="submit" value="Go">196        </form>197    </div>198                     |199                </li>200            <li class="right">201<label class="theme-selector-label">202    Theme203    <select class="theme-selector" oninput="activateTheme(this.value)">204        <option value="auto" selected>Auto</option>205        <option value="light">Light</option>206        <option value="dark">Dark</option>207    </select>208</label> |</li>209            210      </ul>211    </div>    212 213    <div class="document">214      <div class="documentwrapper">215        <div class="bodywrapper">216          <div class="body" role="main">217            218  <section id="module-zipimport">219<span id="zipimport-import-modules-from-zip-archives"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipimport</span></code> — Import modules from Zip archives<a class="headerlink" href="#module-zipimport" title="Link to this heading">¶</a></h1>220<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/zipimport.py">Lib/zipimport.py</a></p>221<hr class="docutils" />222<p>This module adds the ability to import Python modules (<code class="file docutils literal notranslate"><span class="pre">*.py</span></code>,223<code class="file docutils literal notranslate"><span class="pre">*.pyc</span></code>) and packages from ZIP-format archives. It is usually not224needed to use the <code class="xref py py-mod docutils literal notranslate"><span class="pre">zipimport</span></code> module explicitly; it is automatically used225by 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 paths226to ZIP archives.</p>227<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 module228also allows an item of <code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code> to be a string naming a ZIP file archive.229The ZIP archive can contain a subdirectory structure to support package imports,230and a path within the archive can be specified to only import from a231subdirectory.  For example, the path <code class="file docutils literal notranslate"><span class="pre">example.zip/lib/</span></code> would only232import from the <code class="file docutils literal notranslate"><span class="pre">lib/</span></code> subdirectory within the archive.</p>233<p>Any files may be present in the ZIP archive, but importers are only invoked for234<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> files.  ZIP import of dynamic modules235(<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 contains236<code class="file docutils literal notranslate"><span class="pre">.py</span></code> files, Python will not attempt to modify the archive by adding the237corresponding <code class="file docutils literal notranslate"><span class="pre">.pyc</span></code> file, meaning that if a ZIP archive238doesn’t contain <code class="file docutils literal notranslate"><span class="pre">.pyc</span></code> files, importing may be rather slow.</p>239<div class="versionchanged">240<p><span class="versionmodified changed">Changed in version 3.15: </span>Zstandard (<em>zstd</em>) compressed zip file entries are supported.</p>241</div>242<div class="versionchanged">243<p><span class="versionmodified changed">Changed in version 3.13: </span>ZIP64 is supported</p>244</div>245<div class="versionchanged">246<p><span class="versionmodified changed">Changed in version 3.8: </span>Previously, ZIP archives with an archive comment were not supported.</p>247</div>248<div class="admonition seealso">249<p class="admonition-title">See also</p>250<dl class="simple">251<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 and252algorithms used.</p>253</dd>254<dt><span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/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.3255follows the specification in <span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0273/"><strong>PEP 273</strong></a>, but uses an implementation written by Just256van Rossum that uses the import hooks described in <span class="target" id="index-2"></span><a class="pep reference external" href="https://peps.python.org/pep-0302/"><strong>PEP 302</strong></a>.</p>257</dd>258<dt><a class="reference internal" href="importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a> - The implementation of the import machinery</dt><dd><p>Package providing the relevant protocols for all importers to259implement.</p>260</dd>261</dl>262</div>263<p>This module defines an exception:</p>264<dl class="py exception">265<dt class="sig sig-object py" id="zipimport.ZipImportError">266<em class="property"><span class="k"><span class="pre">exception</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">zipimport.</span></span><span class="sig-name descname"><span class="pre">ZipImportError</span></span><a class="headerlink" href="#zipimport.ZipImportError" title="Link to this definition">¶</a></dt>267<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>,268so it can be caught as <code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code>, too.</p>269</dd></dl>270 271<section id="zipimporter-objects">272<span id="id1"></span><h2>zipimporter Objects<a class="headerlink" href="#zipimporter-objects" title="Link to this heading">¶</a></h2>273<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>274<dl class="py class">275<dt class="sig sig-object py" id="zipimport.zipimporter">276<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">zipimport.</span></span><span class="sig-name descname"><span class="pre">zipimporter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">archivepath</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipimport.zipimporter" title="Link to this definition">¶</a></dt>277<dd><p>Create a new zipimporter instance. <em>archivepath</em> must be a path to a ZIP278file, or to a specific path within a ZIP file.  For example, an <em>archivepath</em>279of <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> directory280inside the ZIP file <code class="file docutils literal notranslate"><span class="pre">foo/bar.zip</span></code> (provided that it exists).</p>281<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 ZIP282archive.</p>283<div class="versionchanged">284<p><span class="versionmodified changed">Changed in version 3.12: </span>Methods <code class="docutils literal notranslate"><span class="pre">find_loader()</span></code> and <code class="docutils literal notranslate"><span class="pre">find_module()</span></code>, deprecated in 3.10 are285now removed.  Use <a class="reference internal" href="#zipimport.zipimporter.find_spec" title="zipimport.zipimporter.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_spec()</span></code></a> instead.</p>286</div>287<dl class="py method">288<dt class="sig sig-object py" id="zipimport.zipimporter.create_module">289<span class="sig-name descname"><span class="pre">create_module</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">spec</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipimport.zipimporter.create_module" title="Link to this definition">¶</a></dt>290<dd><p>Implementation of <a class="reference internal" href="importlib.html#importlib.abc.Loader.create_module" title="importlib.abc.Loader.create_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.Loader.create_module()</span></code></a> that returns291<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> to explicitly request the default semantics.</p>292<div class="versionadded">293<p><span class="versionmodified added">Added in version 3.10.</span></p>294</div>295</dd></dl>296 297<dl class="py method">298<dt class="sig sig-object py" id="zipimport.zipimporter.exec_module">299<span class="sig-name descname"><span class="pre">exec_module</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">module</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipimport.zipimporter.exec_module" title="Link to this definition">¶</a></dt>300<dd><p>Implementation of <a class="reference internal" href="importlib.html#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.Loader.exec_module()</span></code></a>.</p>301<div class="versionadded">302<p><span class="versionmodified added">Added in version 3.10.</span></p>303</div>304</dd></dl>305 306<dl class="py method">307<dt class="sig sig-object py" id="zipimport.zipimporter.find_spec">308<span class="sig-name descname"><span class="pre">find_spec</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">target</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipimport.zipimporter.find_spec" title="Link to this definition">¶</a></dt>309<dd><p>An implementation of <a class="reference internal" href="importlib.html#importlib.abc.PathEntryFinder.find_spec" title="importlib.abc.PathEntryFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.PathEntryFinder.find_spec()</span></code></a>.</p>310<div class="versionadded">311<p><span class="versionmodified added">Added in version 3.10.</span></p>312</div>313</dd></dl>314 315<dl class="py method">316<dt class="sig sig-object py" id="zipimport.zipimporter.get_code">317<span class="sig-name descname"><span class="pre">get_code</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipimport.zipimporter.get_code" title="Link to this definition">¶</a></dt>318<dd><p>Return the code object for the specified module. Raise319<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 imported.</p>320</dd></dl>321 322<dl class="py method">323<dt class="sig sig-object py" id="zipimport.zipimporter.get_data">324<span class="sig-name descname"><span class="pre">get_data</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">pathname</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipimport.zipimporter.get_data" title="Link to this definition">¶</a></dt>325<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 the326file wasn’t found.</p>327<div class="versionchanged">328<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, it is now an alias 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>329</div>330</dd></dl>331 332<dl class="py method">333<dt class="sig sig-object py" id="zipimport.zipimporter.get_filename">334<span class="sig-name descname"><span class="pre">get_filename</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipimport.zipimporter.get_filename" title="Link to this definition">¶</a></dt>335<dd><p>Return the value <code class="docutils literal notranslate"><span class="pre">__file__</span></code> would be set to if the specified module336was 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 be337imported.</p>338<div class="versionadded">339<p><span class="versionmodified added">Added in version 3.1.</span></p>340</div>341</dd></dl>342 343<dl class="py method">344<dt class="sig sig-object py" id="zipimport.zipimporter.get_source">345<span class="sig-name descname"><span class="pre">get_source</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipimport.zipimporter.get_source" title="Link to this definition">¶</a></dt>346<dd><p>Return the source code for the specified module. Raise347<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, return348<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 source349for it.</p>350</dd></dl>351 352<dl class="py method">353<dt class="sig sig-object py" id="zipimport.zipimporter.is_package">354<span class="sig-name descname"><span class="pre">is_package</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipimport.zipimporter.is_package" title="Link to this definition">¶</a></dt>355<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. Raise356<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>357</dd></dl>358 359<dl class="py method">360<dt class="sig sig-object py" id="zipimport.zipimporter.invalidate_caches">361<span class="sig-name descname"><span class="pre">invalidate_caches</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#zipimport.zipimporter.invalidate_caches" title="Link to this definition">¶</a></dt>362<dd><p>Clear out the internal cache of information about files found within363the ZIP archive.</p>364<div class="versionadded">365<p><span class="versionmodified added">Added in version 3.10.</span></p>366</div>367</dd></dl>368 369<dl class="py attribute">370<dt class="sig sig-object py" id="zipimport.zipimporter.archive">371<span class="sig-name descname"><span class="pre">archive</span></span><a class="headerlink" href="#zipimport.zipimporter.archive" title="Link to this definition">¶</a></dt>372<dd><p>The file name of the importer’s associated ZIP file, without a possible373subpath.</p>374</dd></dl>375 376<dl class="py attribute">377<dt class="sig sig-object py" id="zipimport.zipimporter.prefix">378<span class="sig-name descname"><span class="pre">prefix</span></span><a class="headerlink" href="#zipimport.zipimporter.prefix" title="Link to this definition">¶</a></dt>379<dd><p>The subpath within the ZIP file where modules are searched.  This is the380empty string for zipimporter objects which point to the root of the ZIP381file.</p>382</dd></dl>383 384<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 a385slash, equal the original <em>archivepath</em> argument given to the386<code class="xref py py-class docutils literal notranslate"><span class="pre">zipimporter</span></code> constructor.</p>387</dd></dl>388 389</section>390<section id="examples">391<span id="zipimport-examples"></span><h2>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h2>392<p>Here is an example that imports a module from a ZIP archive - note that the393<code class="xref py py-mod docutils literal notranslate"><span class="pre">zipimport</span></code> module is not explicitly used.</p>394<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>unzip<span class="w"> </span>-l<span class="w"> </span>example_archive.zip395<span class="go">Archive:  example_archive.zip</span>396<span class="go">  Length     Date   Time    Name</span>397<span class="go"> --------    ----   ----    ----</span>398<span class="go">     8467  01-01-00 12:30   example.py</span>399<span class="go"> --------                   -------</span>400<span class="go">     8467                   1 file</span>401</pre></div>402</div>403<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span><span class="w"> </span><span class="nn">sys</span>404<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Add the archive to the front of the module search path</span>405<span class="gp">&gt;&gt;&gt; </span><span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="s1">&#39;example_archive.zip&#39;</span><span class="p">)</span>406<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span><span class="w"> </span><span class="nn">example</span>407<span class="gp">&gt;&gt;&gt; </span><span class="n">example</span><span class="o">.</span><span class="vm">__file__</span>408<span class="go">&#39;example_archive.zip/example.py&#39;</span>409</pre></div>410</div>411</section>412</section>413 414 415            <div class="clearer"></div>416          </div>417        </div>418      </div>419      <div class="sphinxsidebar" role="navigation" aria-label="Main">420        <div class="sphinxsidebarwrapper">421  <div>422    <h3><a href="../contents.html">Table of Contents</a></h3>423    <ul>424<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>425<li><a class="reference internal" href="#zipimporter-objects">zipimporter Objects</a></li>426<li><a class="reference internal" href="#examples">Examples</a></li>427</ul>428</li>429</ul>430 431  </div>432  <div>433    <h4>Previous topic</h4>434    <p class="topless"><a href="modules.html"435                          title="previous chapter">Importing Modules</a></p>436  </div>437  <div>438    <h4>Next topic</h4>439    <p class="topless"><a href="pkgutil.html"440                          title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pkgutil</span></code> — Package extension utility</a></p>441  </div>442  <script>443    document.addEventListener('DOMContentLoaded', () => {444        const title = document.querySelector('meta[property="og:title"]').content;445        const elements = document.querySelectorAll('.improvepage');446        const pageurl = window.location.href.split('?')[0];447        elements.forEach(element => {448            const url = new URL(element.href.split('?')[0].replace("-nojs", ""));449            url.searchParams.set('pagetitle', title);450            url.searchParams.set('pageurl', pageurl);451            url.searchParams.set('pagesource', "library/zipimport.rst");452            element.href = url.toString();453        });454    });455  </script>456  <div role="note" aria-label="source link">457    <h3>This page</h3>458    <ul class="this-page-menu">459      <li><a href="../bugs.html">Report a bug</a></li>460      <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>461      <li>462        <a href="https://github.com/python/cpython/blob/main/Doc/library/zipimport.rst?plain=1"463            rel="nofollow">Show source464        </a>465      </li>466      467    </ul>468  </div>469        </div>470<div id="sidebarbutton" title="Collapse sidebar">471<span>«</span>472</div>473 474      </div>475      <div class="clearer"></div>476    </div>  477    <div class="related" role="navigation" aria-label="Related">478      <h3>Navigation</h3>479      <ul>480        <li class="right" style="margin-right: 10px">481          <a href="../genindex.html" title="General Index"482             >index</a></li>483        <li class="right" >484          <a href="../py-modindex.html" title="Python Module Index"485             >modules</a> |</li>486        <li class="right" >487          <a href="pkgutil.html" title="pkgutil — Package extension utility"488             >next</a> |</li>489        <li class="right" >490          <a href="modules.html" title="Importing Modules"491             >previous</a> |</li>492 493          <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>494          <li><a href="https://www.python.org/">Python</a> &#187;</li>495          <li class="switchers">496            <div class="language_switcher_placeholder"></div>497            <div class="version_switcher_placeholder"></div>498          </li>499          <li>500              501          </li>502    <li id="cpython-language-and-version">503      <a href="../index.html">3.15.0a6 Documentation</a> &#187;504    </li>505 506          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>507          <li class="nav-item nav-item-2"><a href="modules.html" >Importing Modules</a> &#187;</li>508        <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipimport</span></code> — Import modules from Zip archives</a></li>509                <li class="right">510                    511 512    <div class="inline-search" role="search">513        <form class="inline-search" action="../search.html" method="get">514          <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">515          <input type="submit" value="Go">516        </form>517    </div>518                     |519                </li>520            <li class="right">521<label class="theme-selector-label">522    Theme523    <select class="theme-selector" oninput="activateTheme(this.value)">524        <option value="auto" selected>Auto</option>525        <option value="light">Light</option>526        <option value="dark">Dark</option>527    </select>528</label> |</li>529            530      </ul>531    </div>  532    <div class="footer">533    &copy; <a href="../copyright.html">Copyright</a> 2001 Python Software Foundation.534    <br>535    This page is licensed under the Python Software Foundation License Version 2.536    <br>537    Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.538    <br>539    540      See <a href="/license.html">History and License</a> for more information.<br>541    542    543    <br>544 545    The Python Software Foundation is a non-profit corporation.546<a href="https://www.python.org/psf/donations/">Please donate.</a>547<br>548    <br>549      Last updated on Mar 10, 2026 (08:58 UTC).550    551      <a href="/bugs.html">Found a bug</a>?552    553    <br>554 555    Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.556    </div>557 558  </body>559</html>