diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/python~3.12/library%2Fzipimport.html | |
new repository
Diffstat (limited to 'devdocs/python~3.12/library%2Fzipimport.html')
| -rw-r--r-- | devdocs/python~3.12/library%2Fzipimport.html | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/devdocs/python~3.12/library%2Fzipimport.html b/devdocs/python~3.12/library%2Fzipimport.html new file mode 100644 index 00000000..33cb4e7e --- /dev/null +++ b/devdocs/python~3.12/library%2Fzipimport.html @@ -0,0 +1,70 @@ + <span id="zipimport-import-modules-from-zip-archives"></span><h1>zipimport — Import modules from Zip archives</h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/zipimport.py">Lib/zipimport.py</a></p> <p>This module adds the ability to import Python modules (<code>*.py</code>, <code>*.pyc</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>zipimport</code></a> module explicitly; it is automatically used by the built-in <a class="reference internal" href="../reference/simple_stmts#import"><code>import</code></a> mechanism for <a class="reference internal" href="sys#sys.path" title="sys.path"><code>sys.path</code></a> items that are paths to ZIP archives.</p> <p>Typically, <a class="reference internal" href="sys#sys.path" title="sys.path"><code>sys.path</code></a> is a list of directory names as strings. This module also allows an item of <a class="reference internal" href="sys#sys.path" title="sys.path"><code>sys.path</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>example.zip/lib/</code> would only import from the <code>lib/</code> subdirectory within the archive.</p> <p>Any files may be present in the ZIP archive, but importers are only invoked for <code>.py</code> and <code>.pyc</code> files. ZIP import of dynamic modules (<code>.pyd</code>, <code>.so</code>) is disallowed. Note that if an archive only contains <code>.py</code> files, Python will not attempt to modify the archive by adding the corresponding <code>.pyc</code> file, meaning that if a ZIP archive doesn’t contain <code>.pyc</code> files, importing may be rather slow.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span>Previously, ZIP archives with an archive comment were not supported.</p> </div> <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://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.3 follows 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 Just van 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> </dd> <dt> +<code>importlib - The implementation of the import machinery</code> </dt> +<dd> +<p>Package providing the relevant protocols for all importers to implement.</p> </dd> </dl> </div> <p>This module defines an exception:</p> <dl class="py exception"> <dt class="sig sig-object py" id="zipimport.ZipImportError"> +<code>exception zipimport.ZipImportError</code> </dt> <dd> +<p>Exception raised by zipimporter objects. It’s a subclass of <a class="reference internal" href="exceptions#ImportError" title="ImportError"><code>ImportError</code></a>, so it can be caught as <a class="reference internal" href="exceptions#ImportError" title="ImportError"><code>ImportError</code></a>, too.</p> </dd> +</dl> <section id="zipimporter-objects"> <span id="id1"></span><h2>zipimporter Objects</h2> <p><a class="reference internal" href="#zipimport.zipimporter" title="zipimport.zipimporter"><code>zipimporter</code></a> is the class for importing ZIP files.</p> <dl class="py class"> <dt class="sig sig-object py" id="zipimport.zipimporter"> +<code>class zipimport.zipimporter(archivepath)</code> </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>foo/bar.zip/lib</code> will look for modules in the <code>lib</code> directory inside the ZIP file <code>foo/bar.zip</code> (provided that it exists).</p> <p><a class="reference internal" href="#zipimport.ZipImportError" title="zipimport.ZipImportError"><code>ZipImportError</code></a> is raised if <em>archivepath</em> doesn’t point to a valid ZIP archive.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.12: </span>Methods <code>find_loader()</code> and <code>find_module()</code>, deprecated in 3.10 are now removed. Use <a class="reference internal" href="#zipimport.zipimporter.find_spec" title="zipimport.zipimporter.find_spec"><code>find_spec()</code></a> instead.</p> </div> <dl class="py method"> <dt class="sig sig-object py" id="zipimport.zipimporter.create_module"> +<code>create_module(spec)</code> </dt> <dd> +<p>Implementation of <a class="reference internal" href="importlib#importlib.abc.Loader.create_module" title="importlib.abc.Loader.create_module"><code>importlib.abc.Loader.create_module()</code></a> that returns <a class="reference internal" href="constants#None" title="None"><code>None</code></a> to explicitly request the default semantics.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.10.</span></p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipimport.zipimporter.exec_module"> +<code>exec_module(module)</code> </dt> <dd> +<p>Implementation of <a class="reference internal" href="importlib#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code>importlib.abc.Loader.exec_module()</code></a>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.10.</span></p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipimport.zipimporter.find_spec"> +<code>find_spec(fullname, target=None)</code> </dt> <dd> +<p>An implementation of <a class="reference internal" href="importlib#importlib.abc.PathEntryFinder.find_spec" title="importlib.abc.PathEntryFinder.find_spec"><code>importlib.abc.PathEntryFinder.find_spec()</code></a>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.10.</span></p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipimport.zipimporter.get_code"> +<code>get_code(fullname)</code> </dt> <dd> +<p>Return the code object for the specified module. Raise <a class="reference internal" href="#zipimport.ZipImportError" title="zipimport.ZipImportError"><code>ZipImportError</code></a> if the module couldn’t be imported.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipimport.zipimporter.get_data"> +<code>get_data(pathname)</code> </dt> <dd> +<p>Return the data associated with <em>pathname</em>. Raise <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</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#IOError" title="IOError"><code>IOError</code></a> used to be raised, it is now an alias of <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a>.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipimport.zipimporter.get_filename"> +<code>get_filename(fullname)</code> </dt> <dd> +<p>Return the value <code>__file__</code> would be set to if the specified module was imported. Raise <a class="reference internal" href="#zipimport.ZipImportError" title="zipimport.ZipImportError"><code>ZipImportError</code></a> if the module couldn’t be imported.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.1.</span></p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipimport.zipimporter.get_source"> +<code>get_source(fullname)</code> </dt> <dd> +<p>Return the source code for the specified module. Raise <a class="reference internal" href="#zipimport.ZipImportError" title="zipimport.ZipImportError"><code>ZipImportError</code></a> if the module couldn’t be found, return <a class="reference internal" href="constants#None" title="None"><code>None</code></a> if the archive does contain the module, but has no source for it.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipimport.zipimporter.is_package"> +<code>is_package(fullname)</code> </dt> <dd> +<p>Return <code>True</code> if the module specified by <em>fullname</em> is a package. Raise <a class="reference internal" href="#zipimport.ZipImportError" title="zipimport.ZipImportError"><code>ZipImportError</code></a> if the module couldn’t be found.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipimport.zipimporter.load_module"> +<code>load_module(fullname)</code> </dt> <dd> +<p>Load the module specified by <em>fullname</em>. <em>fullname</em> must be the fully qualified (dotted) module name. Returns the imported module on success, raises <a class="reference internal" href="#zipimport.ZipImportError" title="zipimport.ZipImportError"><code>ZipImportError</code></a> on failure.</p> <div class="deprecated"> <p><span class="versionmodified deprecated">Deprecated since version 3.10: </span>Use <a class="reference internal" href="#zipimport.zipimporter.exec_module" title="zipimport.zipimporter.exec_module"><code>exec_module()</code></a> instead.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipimport.zipimporter.invalidate_caches"> +<code>invalidate_caches()</code> </dt> <dd> +<p>Clear out the internal cache of information about files found within the ZIP archive.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.10.</span></p> </div> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="zipimport.zipimporter.archive"> +<code>archive</code> </dt> <dd> +<p>The file name of the importer’s associated ZIP file, without a possible subpath.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="zipimport.zipimporter.prefix"> +<code>prefix</code> </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>archive</code></a> and <a class="reference internal" href="#zipimport.zipimporter.prefix" title="zipimport.zipimporter.prefix"><code>prefix</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>zipimporter</code></a> constructor.</p> </dd> +</dl> </section> <section id="examples"> <span id="zipimport-examples"></span><h2>Examples</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>zipimport</code></a> module is not explicitly used.</p> <pre data-language="shell">$ 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> </section> <div class="_attribution"> + <p class="_attribution-p"> + © 2001–2023 Python Software Foundation<br>Licensed under the PSF License.<br> + <a href="https://docs.python.org/3.12/library/zipimport.html" class="_attribution-link">https://docs.python.org/3.12/library/zipimport.html</a> + </p> +</div> |
