uTools-Manuals/docs/python/zipimport.html
2019-04-08 23:22:26 +08:00

15 lines
15 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<div class="body" role="main"><div class="section" id="module-zipimport"><h1><span class="yiyi-st" id="yiyi-10">31.1. <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"><span class="pre">zipimport</span></code></a> - 从Zip档案导入模块</span></h1><p><span class="yiyi-st" id="yiyi-11">此模块添加了从ZIP格式的归档中导入Python模块<code class="file docutils literal"><span class="pre">*.py</span></code><code class="file docutils literal"><span class="pre">*.py[co]</span></code>)和软件包的功能。</span><span class="yiyi-st" id="yiyi-12">通常不需要显式地使用<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"><span class="pre">zipimport</span></code></a>模块;它由内部<a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal"><span class="pre">import</span></code></a>机制自动用于作为ZIP压缩文件路径的<a class="reference internal" href="sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal"><span class="pre">sys.path</span></code></a>项目。</span></p><p><span class="yiyi-st" id="yiyi-13">通常,<a class="reference internal" href="sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal"><span class="pre">sys.path</span></code></a>是作为字符串的目录名称列表。</span><span class="yiyi-st" id="yiyi-14">该模块还允许<a class="reference internal" href="sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal"><span class="pre">sys.path</span></code></a>的项目是一个命名ZIP文件归档的字符串。</span><span class="yiyi-st" id="yiyi-15">ZIP存档可以包含一个子目录结构以支持包导入并且可以指定存档内的路径仅从子目录导入。</span><span class="yiyi-st" id="yiyi-16">例如,路径<code class="file docutils literal"><span class="pre">example.zip/lib/</span></code>将只从归档中的<code class="file docutils literal"><span class="pre">lib/</span></code>子目录导入。</span></p><p><span class="yiyi-st" id="yiyi-17">ZIP存档中可能存在任何文件但只有文件<code class="file docutils literal"><span class="pre">.py</span></code><code class="file docutils literal"><span class="pre">.pyc</span></code>可供导入。</span><span class="yiyi-st" id="yiyi-18">不允许动态模块的ZIP导入<code class="file docutils literal"><span class="pre">.pyd</span></code><code class="file docutils literal"><span class="pre">.so</span></code>)。</span><span class="yiyi-st" id="yiyi-19">请注意,如果归档只包含<code class="file docutils literal"><span class="pre">.py</span></code>文件则Python不会尝试通过添加相应的<code class="file docutils literal"><span class="pre">.pyc</span></code>文件来修改归档文件这意味着如果ZIP归档文件不存在包含<code class="file docutils literal"><span class="pre">.pyc</span></code>文件,导入可能会相当缓慢。</span></p><p><span class="yiyi-st" id="yiyi-20">目前不支持具有归档注释的ZIP归档。</span></p><div class="admonition seealso"><p class="first admonition-title"><span class="yiyi-st" id="yiyi-21">也可以看看</span></p><dl class="last docutils"><dt><span class="yiyi-st" id="yiyi-22"><a class="reference external" href="https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT">PKZIP应用笔记</a></span></dt><dd><span class="yiyi-st" id="yiyi-23">关于ZIP文件格式的文档由Phil Katz使用的格式和算法的创建者。</span></dd><dt><span class="yiyi-st" id="yiyi-24"><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> - 从Zip档案库导入模块</span></dt><dd><span class="yiyi-st" id="yiyi-25">作者James C. Ahlstrom他也提供了一个实现。</span><span class="yiyi-st" id="yiyi-26">Python 2.3遵循PEP 273中的规范但使用由Just van Rossum编写的使用PEP 302中描述的导入钩子的实现。</span></dd><dt><span class="yiyi-st" id="yiyi-27"><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> - 新的导入挂钩</span></dt><dd><span class="yiyi-st" id="yiyi-28">PEP添加了帮助这个模块工作的导入钩子。</span></dd></dl></div><p><span class="yiyi-st" id="yiyi-29">此模块定义了一个例外:</span></p><dl class="exception"><dt id="zipimport.ZipImportError"><span class="yiyi-st" id="yiyi-30"> <em class="property">exception </em><code class="descclassname">zipimport.</code><code class="descname">ZipImportError</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-31">zipimporter对象引发的异常。</span><span class="yiyi-st" id="yiyi-32">它是<a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal"><span class="pre">ImportError</span></code></a>的子类,因此也可以作为<a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal"><span class="pre">ImportError</span></code></a>被捕获。</span></p></dd></dl><div class="section" id="zipimporter-objects"><h2><span class="yiyi-st" id="yiyi-33">31.1.1. zipimporter对象</span></h2><p><span class="yiyi-st" id="yiyi-34"><a class="reference internal" href="#zipimport.zipimporter" title="zipimport.zipimporter"><code class="xref py py-class docutils literal"><span class="pre">zipimporter</span></code></a>是用于导入ZIP文件的类。</span></p><dl class="class"><dt id="zipimport.zipimporter"><span class="yiyi-st" id="yiyi-35"> <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></span></dt><dd><p><span class="yiyi-st" id="yiyi-36">创建一个新的zipimporter实例。</span><span class="yiyi-st" id="yiyi-37"><em>archivepath</em>必须是ZIP文件或ZIP文件中特定路径的路径。</span><span class="yiyi-st" id="yiyi-38">For example, an <em>archivepath</em> of <code class="file docutils literal"><span class="pre">foo/bar.zip/lib</span></code> will look for modules in the <code class="file docutils literal"><span class="pre">lib</span></code> directory inside the ZIP file <code class="file docutils literal"><span class="pre">foo/bar.zip</span></code> (provided that it exists).</span></p><p><span class="yiyi-st" id="yiyi-39">如果<em>archivepath</em>未指向有效的ZIP压缩文件则会引发<a class="reference internal" href="#zipimport.ZipImportError" title="zipimport.ZipImportError"><code class="xref py py-exc docutils literal"><span class="pre">ZipImportError</span></code></a></span></p><dl class="method"><dt id="zipimport.zipimporter.find_module"><span class="yiyi-st" id="yiyi-40"> <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></span></dt><dd><p><span class="yiyi-st" id="yiyi-41">搜索由<em>fullname</em>指定的模块。</span><span class="yiyi-st" id="yiyi-42"><em>fullname</em>必须是完全限定(虚线)模块名称。</span><span class="yiyi-st" id="yiyi-43">如果找到模块则返回zipimporter实例本身如果不是则返回<a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal"><span class="pre">None</span></code></a></span><span class="yiyi-st" id="yiyi-44">可选的<em>路径</em>参数被忽略 - 它与导入器协议兼容。</span></p></dd></dl><dl class="method"><dt id="zipimport.zipimporter.get_code"><span class="yiyi-st" id="yiyi-45"> <code class="descname">get_code</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-46">返回指定模块的代码对象。</span><span class="yiyi-st" id="yiyi-47">如果找不到模块,则引发<a class="reference internal" href="#zipimport.ZipImportError" title="zipimport.ZipImportError"><code class="xref py py-exc docutils literal"><span class="pre">ZipImportError</span></code></a></span></p></dd></dl><dl class="method"><dt id="zipimport.zipimporter.get_data"><span class="yiyi-st" id="yiyi-48"> <code class="descname">get_data</code><span class="sig-paren">(</span><em>pathname</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-49">返回与<em>路径名</em>关联的数据。</span><span class="yiyi-st" id="yiyi-50">如果找不到文件,则引发<a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal"><span class="pre">OSError</span></code></a></span></p><div class="versionchanged"><p><span class="yiyi-st" id="yiyi-51"><span class="versionmodified">在版本3.3中更改:</span> <a class="reference internal" href="exceptions.html#IOError" title="IOError"><code class="xref py py-exc docutils literal"><span class="pre">IOError</span></code></a>已升级,而不是<a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal"><span class="pre">OSError</span></code></a></span></p></div></dd></dl><dl class="method"><dt id="zipimport.zipimporter.get_filename"><span class="yiyi-st" id="yiyi-52"> <code class="descname">get_filename</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-53">返回值<code class="docutils literal"><span class="pre">__file__</span></code>将被设置为如果指定的模块被导入。</span><span class="yiyi-st" id="yiyi-54">如果找不到模块,则引发<a class="reference internal" href="#zipimport.ZipImportError" title="zipimport.ZipImportError"><code class="xref py py-exc docutils literal"><span class="pre">ZipImportError</span></code></a></span></p><div class="versionadded"><p><span class="yiyi-st" id="yiyi-55"><span class="versionmodified">版本3.1中的新功能。</span></span></p></div></dd></dl><dl class="method"><dt id="zipimport.zipimporter.get_source"><span class="yiyi-st" id="yiyi-56"> <code class="descname">get_source</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-57">返回指定模块的源代码。</span><span class="yiyi-st" id="yiyi-58">引发<a class="reference internal" href="#zipimport.ZipImportError" title="zipimport.ZipImportError"><code class="xref py py-exc docutils literal"><span class="pre">ZipImportError</span></code></a>如果找不到模块,则返回<a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal"><span class="pre">None</span></code></a>,如果存档包含模块,但没有源。</span></p></dd></dl><dl class="method"><dt id="zipimport.zipimporter.is_package"><span class="yiyi-st" id="yiyi-59"> <code class="descname">is_package</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-60">如果<em>fullname</em>指定的模块是包,则返回<code class="docutils literal"><span class="pre">True</span></code></span><span class="yiyi-st" id="yiyi-61">如果找不到模块,则引发<a class="reference internal" href="#zipimport.ZipImportError" title="zipimport.ZipImportError"><code class="xref py py-exc docutils literal"><span class="pre">ZipImportError</span></code></a></span></p></dd></dl><dl class="method"><dt id="zipimport.zipimporter.load_module"><span class="yiyi-st" id="yiyi-62"> <code class="descname">load_module</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-63">加载<em>fullname</em>指定的模块。</span><span class="yiyi-st" id="yiyi-64"><em>fullname</em>必须是完全限定(虚线)模块名称。</span><span class="yiyi-st" id="yiyi-65">它返回导入的模块,如果找不到,则返回引发<a class="reference internal" href="#zipimport.ZipImportError" title="zipimport.ZipImportError"><code class="xref py py-exc docutils literal"><span class="pre">ZipImportError</span></code></a></span></p></dd></dl><dl class="attribute"><dt id="zipimport.zipimporter.archive"><span class="yiyi-st" id="yiyi-66"> <code class="descname">archive</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-67">导入程序的关联ZIP文件的文件名没有可能的子路径。</span></p></dd></dl><dl class="attribute"><dt id="zipimport.zipimporter.prefix"><span class="yiyi-st" id="yiyi-68"> <code class="descname">prefix</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-69">ZIP文件中搜索模块的子路径。</span><span class="yiyi-st" id="yiyi-70">这是指向ZIP文件根目录的zipimporter对象的空字符串。</span></p></dd></dl><p><span class="yiyi-st" id="yiyi-71">与斜杠组合使用时,<a class="reference internal" href="#zipimport.zipimporter.archive" title="zipimport.zipimporter.archive"><code class="xref py py-attr docutils literal"><span class="pre">archive</span></code></a><a class="reference internal" href="#zipimport.zipimporter.prefix" title="zipimport.zipimporter.prefix"><code class="xref py py-attr docutils literal"><span class="pre">prefix</span></code></a>属性等于给予<a class="reference internal" href="#zipimport.zipimporter" title="zipimport.zipimporter"><code class="xref py py-class docutils literal"><span class="pre">zipimporter</span></code></a>构造函数的原始<em>archivepath</em></span></p></dd></dl></div><div class="section" id="examples"><h2><span class="yiyi-st" id="yiyi-72">31.1.2. </span><span class="yiyi-st" id="yiyi-73">Examples</span></h2><p><span class="yiyi-st" id="yiyi-74">以下是从ZIP存档导入模块的示例 - 请注意,未明确使用<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"><span class="pre">zipimport</span></code></a>模块。</span></p><div class="highlight-shell-session"><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">&gt;</span>&gt;&gt; import sys
<span class="gp">&gt;</span>&gt;&gt; sys.path.insert<span class="o">(</span>0, <span class="s1">'example.zip'</span><span class="o">)</span> <span class="c1"># Add .zip file to front of path</span>
<span class="gp">&gt;</span>&gt;&gt; import jwzthreading
<span class="gp">&gt;</span>&gt;&gt; jwzthreading.__file__
<span class="go">'example.zip/jwzthreading.py'</span>
</pre></div></div></div></div></div>