477 lines
36 KiB
HTML
477 lines
36 KiB
HTML
|
||
<!DOCTYPE html>
|
||
|
||
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
|
||
<head>
|
||
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
|
||
|
||
<title>compileall --- 字节编译 Python 库 — Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
|
||
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
|
||
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
||
|
||
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
|
||
<script src="../_static/jquery.js"></script>
|
||
<script src="../_static/underscore.js"></script>
|
||
<script src="../_static/doctools.js"></script>
|
||
<script src="../_static/language_data.js"></script>
|
||
<script src="../_static/translations.js"></script>
|
||
|
||
<script src="../_static/sidebar.js"></script>
|
||
|
||
<link rel="search" type="application/opensearchdescription+xml"
|
||
title="在 Python 3.8.20 文档 中搜索"
|
||
href="../_static/opensearch.xml"/>
|
||
<link rel="author" title="关于这些文档" href="../about.html" />
|
||
<link rel="index" title="索引" href="../genindex.html" />
|
||
<link rel="search" title="搜索" href="../search.html" />
|
||
<link rel="copyright" title="版权所有" href="../copyright.html" />
|
||
<link rel="next" title="dis --- Python 字节码反汇编器" href="dis.html" />
|
||
<link rel="prev" title="py_compile --- 编译 Python 源文件" href="py_compile.html" />
|
||
<link rel="canonical" href="https://docs.python.org/3/library/compileall.html" />
|
||
|
||
|
||
|
||
|
||
|
||
<style>
|
||
@media only screen {
|
||
table.full-width-table {
|
||
width: 100%;
|
||
}
|
||
}
|
||
</style>
|
||
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
|
||
<script type="text/javascript" src="../_static/copybutton.js"></script>
|
||
<script type="text/javascript" src="../_static/menu.js"></script>
|
||
|
||
</head>
|
||
<body>
|
||
<div class="mobile-nav">
|
||
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
|
||
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
|
||
<label for="menuToggler" class="toggler__label">
|
||
<span></span>
|
||
</label>
|
||
<nav class="nav-content" role="navigation">
|
||
<a href="https://www.python.org/" class="nav-logo">
|
||
<img src="../_static/py.svg" alt="Logo"/>
|
||
</a>
|
||
<div class="version_switcher_placeholder"></div>
|
||
<form role="search" class="search" action="../search.html" method="get">
|
||
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
|
||
<path fill-rule="nonzero"
|
||
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" fill="#444"></path>
|
||
</svg>
|
||
<input type="text" name="q" aria-label="快速搜索"/>
|
||
<input type="submit" value="转向"/>
|
||
</form>
|
||
</nav>
|
||
<div class="menu-wrapper">
|
||
<nav class="menu" role="navigation" aria-label="main navigation">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<h3><a href="../contents.html">目录</a></h3>
|
||
<ul>
|
||
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">compileall</span></code> --- 字节编译 Python 库</a><ul>
|
||
<li><a class="reference internal" href="#command-line-use">使用命令行</a></li>
|
||
<li><a class="reference internal" href="#public-functions">公有函数</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="py_compile.html"
|
||
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">py_compile</span></code> --- 编译 Python 源文件</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="dis.html"
|
||
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dis</span></code> --- Python 字节码反汇编器</a></p>
|
||
<div role="note" aria-label="source link">
|
||
<h3>本页</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">报告 Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/compileall.rst"
|
||
rel="nofollow">显示源代码
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</nav>
|
||
</div>
|
||
</div>
|
||
|
||
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
|
||
这个文档所针对的是一个已不再受支持的 Python 旧版本。
|
||
你应当升级版本,并阅读
|
||
<a href="/3/library/compileall.html"> Python 当前稳定版本的文档</a>.
|
||
</div>
|
||
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>导航</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="总目录"
|
||
accesskey="I">索引</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python 模块索引"
|
||
>模块</a> |</li>
|
||
<li class="right" >
|
||
<a href="dis.html" title="dis --- Python 字节码反汇编器"
|
||
accesskey="N">下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="py_compile.html" title="py_compile --- 编译 Python 源文件"
|
||
accesskey="P">上一页</a> |</li>
|
||
|
||
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
|
||
<li><a href="https://www.python.org/">Python</a> »</li>
|
||
<li class="switchers">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<div class="version_switcher_placeholder"></div>
|
||
</li>
|
||
<li>
|
||
|
||
</li>
|
||
<li id="cpython-language-and-version">
|
||
<a href="../index.html">3.8.20 Documentation</a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> »</li>
|
||
<li class="nav-item nav-item-2"><a href="language.html" accesskey="U">Python 语言服务</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
|
||
<input type="submit" value="转向" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
|
||
<div class="document">
|
||
<div class="documentwrapper">
|
||
<div class="bodywrapper">
|
||
<div class="body" role="main">
|
||
|
||
<section id="module-compileall">
|
||
<span id="compileall-byte-compile-python-libraries"></span><h1><a class="reference internal" href="#module-compileall" title="compileall: Tools for byte-compiling all Python source files in a directory tree."><code class="xref py py-mod docutils literal notranslate"><span class="pre">compileall</span></code></a> --- 字节编译 Python 库<a class="headerlink" href="#module-compileall" title="永久链接至标题">¶</a></h1>
|
||
<p><strong>源代码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.8/Lib/compileall.py">Lib/compileall.py</a></p>
|
||
<hr class="docutils" />
|
||
<p>这个模块提供了一些工具函数来支持安装 Python 库。 这些函数可以编译一个目录树中的 Python 源文件。 这个模块可被用来在安装库时创建缓存的字节码文件,这使得它们对于没有库目录写入权限的用户来说也是可用的。</p>
|
||
<section id="command-line-use">
|
||
<h2>使用命令行<a class="headerlink" href="#command-line-use" title="永久链接至标题">¶</a></h2>
|
||
<p>此模块可以作为脚本运行 (使用 <strong class="program">python -m compileall</strong>) 来编译 Python 源代码。</p>
|
||
<dl class="cmdoption">
|
||
<dt id="cmdoption-compileall-arg-directory">
|
||
<code class="sig-name descname">directory</code><code class="sig-prename descclassname"> ...</code><a class="headerlink" href="#cmdoption-compileall-arg-directory" title="永久链接至目标">¶</a></dt>
|
||
<dt id="cmdoption-compileall-arg-file">
|
||
<code class="sig-name descname">file</code><code class="sig-prename descclassname"> ...</code><a class="headerlink" href="#cmdoption-compileall-arg-file" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>位置参数是要编译的文件或包含源文件的目录,目录将被递归地遍历。 如果没有给出参数,则其行为如同使用了命令行 <code class="docutils literal notranslate"><span class="pre">-l</span> <span class="pre"><directories</span> <span class="pre">from</span> <span class="pre">sys.path></span></code>。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="cmdoption">
|
||
<dt id="cmdoption-compileall-l">
|
||
<code class="sig-name descname">-l</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#cmdoption-compileall-l" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>不要递归到子目录,只编译直接包含在指明或隐含的目录中的源代码文件。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="cmdoption">
|
||
<dt id="cmdoption-compileall-f">
|
||
<code class="sig-name descname">-f</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#cmdoption-compileall-f" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>强制重新构建即使时间戳是最新的。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="cmdoption">
|
||
<dt id="cmdoption-compileall-q">
|
||
<code class="sig-name descname">-q</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#cmdoption-compileall-q" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>不要打印已编译文件的列表。 如果传入一次,则错误消息仍将被打印。 如果传入两次 (<code class="docutils literal notranslate"><span class="pre">-qq</span></code>),所有输出都会被屏蔽。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="cmdoption">
|
||
<dt id="cmdoption-compileall-d">
|
||
<code class="sig-name descname">-d</code><code class="sig-prename descclassname"> destdir</code><a class="headerlink" href="#cmdoption-compileall-d" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>要附加到每个被编译文件的路径之前的目录。 这将出现在编译时回溯信息中,并且还会被编译到字节码文件中,届时它将在字节码文件被执行而源文件已不存在的情况下被用于回溯和其他消息。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="cmdoption">
|
||
<dt id="cmdoption-compileall-x">
|
||
<code class="sig-name descname">-x</code><code class="sig-prename descclassname"> regex</code><a class="headerlink" href="#cmdoption-compileall-x" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>regex 会被用于搜索每个要执行编译的文件的完整路径,而如果 regex 产生了一个匹配,则相应文件会被跳过。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="cmdoption">
|
||
<dt id="cmdoption-compileall-i">
|
||
<code class="sig-name descname">-i</code><code class="sig-prename descclassname"> list</code><a class="headerlink" href="#cmdoption-compileall-i" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>读取文件 <code class="docutils literal notranslate"><span class="pre">list</span></code> 并将其包含的每一行添加到要编译的文件和目录列表中。 如果 <code class="docutils literal notranslate"><span class="pre">list</span></code> 为 <code class="docutils literal notranslate"><span class="pre">-</span></code>,则从 <code class="docutils literal notranslate"><span class="pre">stdin</span></code> 读取行。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="cmdoption">
|
||
<dt id="cmdoption-compileall-b">
|
||
<code class="sig-name descname">-b</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#cmdoption-compileall-b" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将字节码写入到它们的传统位置和名称,这可能会覆盖由另一版本的 Python 所创建的字节码文件。 默认是将文件写入到它们的 <span class="target" id="index-8"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3147"><strong>PEP 3147</strong></a> 位置和名称,这允许来自多个版本的 Python 字节码文件共存。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="cmdoption">
|
||
<dt id="cmdoption-compileall-r">
|
||
<code class="sig-name descname">-r</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#cmdoption-compileall-r" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>控制子目录的最大递归层级。 如果给出此选项,则 <code class="docutils literal notranslate"><span class="pre">-l</span></code> 选项将不会被考虑。 <strong class="program">python -m compileall <directory> -r 0</strong> 等价于 <strong class="program">python -m compileall <directory> -l</strong>。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="cmdoption">
|
||
<dt id="cmdoption-compileall-j">
|
||
<code class="sig-name descname">-j</code><code class="sig-prename descclassname"> N</code><a class="headerlink" href="#cmdoption-compileall-j" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>使用 <em>N</em> 个工作者来编译给定目录中的文件。 如果使用 <code class="docutils literal notranslate"><span class="pre">0</span></code>,则将使用 <a class="reference internal" href="os.html#os.cpu_count" title="os.cpu_count"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.cpu_count()</span></code></a> 的结果。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="cmdoption">
|
||
<dt id="cmdoption-compileall-invalidation-mode">
|
||
<code class="sig-name descname">--invalidation-mode</code><code class="sig-prename descclassname"> [timestamp|checked-hash|unchecked-hash]</code><a class="headerlink" href="#cmdoption-compileall-invalidation-mode" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>控制生成的字节码文件在运行时的失效规则。 值为``timestamp``,意味着将生成嵌入了源时间戳和大小的 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 文件。 <code class="docutils literal notranslate"><span class="pre">checked-hash</span></code> 和 <code class="docutils literal notranslate"><span class="pre">unchecked-hash</span></code> 等值将导致生成基于哈希的 pyc。 基于哈希的 pyc 嵌入了源文件内容的哈希值而不是时间戳。 请参阅 <a class="reference internal" href="../reference/import.html#pyc-invalidation"><span class="std std-ref">已缓存字节码的失效</span></a> 了解有关 Python 在运行时如何验证字节码缓存文件的更多信息。 如果未设置 <span class="target" id="index-9"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">SOURCE_DATE_EPOCH</span></code> 环境变量则默认值为 <code class="docutils literal notranslate"><span class="pre">timestamp</span></code>,而如果设置了 <code class="docutils literal notranslate"><span class="pre">SOURCE_DATE_EPOCH</span></code> 则为 <code class="docutils literal notranslate"><span class="pre">checked-hash</span></code>。</p>
|
||
</dd></dl>
|
||
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.2 版更改: </span>增加了 <code class="docutils literal notranslate"><span class="pre">-i</span></code>, <code class="docutils literal notranslate"><span class="pre">-b</span></code> 和 <code class="docutils literal notranslate"><span class="pre">-h</span></code> 选项。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.5 版更改: </span>增加了 <code class="docutils literal notranslate"><span class="pre">-j</span></code>, <code class="docutils literal notranslate"><span class="pre">-r</span></code> 和 <code class="docutils literal notranslate"><span class="pre">-qq</span></code> 选项。 <code class="docutils literal notranslate"><span class="pre">-q</span></code> 选项改为多级别值。 <code class="docutils literal notranslate"><span class="pre">-b</span></code> 将总是产生以 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 为后缀的字节码文件,而不是 <code class="docutils literal notranslate"><span class="pre">.pyo</span></code>。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.7 版更改: </span>增加了 <code class="docutils literal notranslate"><span class="pre">--invalidation-mode</span></code> 选项。</p>
|
||
</div>
|
||
<p>没有可以控制 <a class="reference internal" href="functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a> 函数所使用的优化级别的命令行选项,因为 Python 解释器本身已经提供了该选项: <strong class="program">python -O -m compileall</strong>。</p>
|
||
<p>类似地,<a class="reference internal" href="functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a> 函数会遵循 <a class="reference internal" href="sys.html#sys.pycache_prefix" title="sys.pycache_prefix"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.pycache_prefix</span></code></a> 设置。 所生成的字节码缓存将仅当 <a class="reference internal" href="functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a> 附带与将在运行时使用的相同 <a class="reference internal" href="sys.html#sys.pycache_prefix" title="sys.pycache_prefix"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.pycache_prefix</span></code></a> 时可用(如果存在该设置)。</p>
|
||
</section>
|
||
<section id="public-functions">
|
||
<h2>公有函数<a class="headerlink" href="#public-functions" title="永久链接至标题">¶</a></h2>
|
||
<dl class="function">
|
||
<dt id="compileall.compile_dir">
|
||
<code class="sig-prename descclassname">compileall.</code><code class="sig-name descname">compile_dir</code><span class="sig-paren">(</span><em class="sig-param">dir</em>, <em class="sig-param">maxlevels=10</em>, <em class="sig-param">ddir=None</em>, <em class="sig-param">force=False</em>, <em class="sig-param">rx=None</em>, <em class="sig-param">quiet=0</em>, <em class="sig-param">legacy=False</em>, <em class="sig-param">optimize=-1</em>, <em class="sig-param">workers=1</em>, <em class="sig-param">invalidation_mode=None</em><span class="sig-paren">)</span><a class="headerlink" href="#compileall.compile_dir" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>递归地深入名为 <em>dir</em> 的目录树,在途中编译所有 <code class="file docutils literal notranslate"><span class="pre">.py</span></code> 文件。 如果所有文件都编译成功则返回真值,否则返回假值。</p>
|
||
<p>The <em>maxlevels</em> parameter is used to limit the depth of the recursion; it
|
||
defaults to <code class="docutils literal notranslate"><span class="pre">10</span></code>.</p>
|
||
<p>如果给出了 <em>ddir</em>,它会被附加到每个被编译的文件的路径之前以便在编译时回溯中使用,同时还会被编译到字节码文件中,届时它将在字节码文件被执行而源文件已不存在的情况下被用于回溯和其他消息中。</p>
|
||
<p>如果 <em>force</em> 为真值,则即使时间戳为最新模块也会被重新编译。</p>
|
||
<p>If <em>rx</em> is given, its search method is called on the complete path to each
|
||
file considered for compilation, and if it returns a true value, the file
|
||
is skipped.</p>
|
||
<p>如果 <em>quiet</em> 为 <code class="docutils literal notranslate"><span class="pre">False</span></code> 或 <code class="docutils literal notranslate"><span class="pre">0</span></code> (默认值),则文件名和其他信息将被打印到标准输出。 如果设为 <code class="docutils literal notranslate"><span class="pre">1</span></code>,则只打印错误。 如果设为 <code class="docutils literal notranslate"><span class="pre">2</span></code>,则屏蔽所有输出。</p>
|
||
<p>如果 <em>legacy</em> 为真值,则将字节码文件写入到它们的传统位置和名称,这可能会覆盖由另一版本的 Python 所创建的字节码文件。 默认是将文件写入到它们的 <span class="target" id="index-10"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3147"><strong>PEP 3147</strong></a> 位置和名称,这允许来自多个版本的 Python 字节码文件共存。</p>
|
||
<p><em>optimize</em> specifies the optimization level for the compiler. It is passed to
|
||
the built-in <a class="reference internal" href="functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a> function.</p>
|
||
<p>参数 <em>workers</em> 指明要使用多少个工作进程来并行编译文件。 默认设置不使用多个工作进程。 如果平台不能使用多个工作进程而又给出了 <em>workers</em> 参数,则将回退为使用顺序编译。 如果 <em>workers</em> 为 0,则会使用系统的核心数量。 如果 <em>workers</em> 小于 <code class="docutils literal notranslate"><span class="pre">0</span></code>,则会引发 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>。</p>
|
||
<p><em>invalidation_mode</em> 应为 <a class="reference internal" href="py_compile.html#py_compile.PycInvalidationMode" title="py_compile.PycInvalidationMode"><code class="xref py py-class docutils literal notranslate"><span class="pre">py_compile.PycInvalidationMode</span></code></a> 枚举的成员之一并将控制所生成的 pyc 在运行时以何种方式验证是否失效。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.2 版更改: </span>增加了 <em>legacy</em> 和 <em>optimize</em> 形参。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.5 版更改: </span>增加了 <em>workers</em> 形参。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.5 版更改: </span><em>quiet</em> 形参已改为多级别值。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.5 版更改: </span><em>legacy</em> 形参将只写入 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 文件而非 <code class="docutils literal notranslate"><span class="pre">.pyo</span></code> 文件,无论 <em>optimize</em> 的值是什么。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.7 版更改: </span>增加了 <em>invalidation_mode</em> 形参。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.7.2 版更改: </span><em>invalidation_mode</em> 形参的默认值更新为 None。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.8 版更改: </span>将 <em>workers</em> 设为 0 现在将会选择最优核心数量。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="compileall.compile_file">
|
||
<code class="sig-prename descclassname">compileall.</code><code class="sig-name descname">compile_file</code><span class="sig-paren">(</span><em class="sig-param">fullname</em>, <em class="sig-param">ddir=None</em>, <em class="sig-param">force=False</em>, <em class="sig-param">rx=None</em>, <em class="sig-param">quiet=0</em>, <em class="sig-param">legacy=False</em>, <em class="sig-param">optimize=-1</em>, <em class="sig-param">invalidation_mode=None</em><span class="sig-paren">)</span><a class="headerlink" href="#compileall.compile_file" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>编译路径为 <em>fullname</em> 的文件。 如果文件编译成功则返回真值,否则返回假值。</p>
|
||
<p>如果给出了 <em>ddir</em>,它会被附加到被编译的文件的路径之前以便在编译时回溯中使用,同时还会被编译到字节码文件中,届时它将在字节码文件被执行而源文件已不存在的情况下被用于回溯和其他消息中。</p>
|
||
<p>If <em>rx</em> is given, its search method is passed the full path name to the
|
||
file being compiled, and if it returns a true value, the file is not
|
||
compiled and <code class="docutils literal notranslate"><span class="pre">True</span></code> is returned.</p>
|
||
<p>如果 <em>quiet</em> 为 <code class="docutils literal notranslate"><span class="pre">False</span></code> 或 <code class="docutils literal notranslate"><span class="pre">0</span></code> (默认值),则文件名和其他信息将被打印到标准输出。 如果设为 <code class="docutils literal notranslate"><span class="pre">1</span></code>,则只打印错误。 如果设为 <code class="docutils literal notranslate"><span class="pre">2</span></code>,则屏蔽所有输出。</p>
|
||
<p>如果 <em>legacy</em> 为真值,则将字节码文件写入到它们的传统位置和名称,这可能会覆盖由另一版本的 Python 所创建的字节码文件。 默认是将文件写入到它们的 <span class="target" id="index-11"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3147"><strong>PEP 3147</strong></a> 位置和名称,这允许来自多个版本的 Python 字节码文件共存。</p>
|
||
<p><em>optimize</em> specifies the optimization level for the compiler. It is passed to
|
||
the built-in <a class="reference internal" href="functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a> function.</p>
|
||
<p><em>invalidation_mode</em> 应为 <a class="reference internal" href="py_compile.html#py_compile.PycInvalidationMode" title="py_compile.PycInvalidationMode"><code class="xref py py-class docutils literal notranslate"><span class="pre">py_compile.PycInvalidationMode</span></code></a> 枚举的成员之一并将控制所生成的 pyc 在运行时以何种方式验证是否失效。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.2 新版功能.</span></p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.5 版更改: </span><em>quiet</em> 形参已改为多级别值。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.5 版更改: </span><em>legacy</em> 形参将只写入 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 文件而非 <code class="docutils literal notranslate"><span class="pre">.pyo</span></code> 文件,无论 <em>optimize</em> 的值是什么。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.7 版更改: </span>增加了 <em>invalidation_mode</em> 形参。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.7.2 版更改: </span><em>invalidation_mode</em> 形参的默认值更新为 None。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="compileall.compile_path">
|
||
<code class="sig-prename descclassname">compileall.</code><code class="sig-name descname">compile_path</code><span class="sig-paren">(</span><em class="sig-param">skip_curdir=True</em>, <em class="sig-param">maxlevels=0</em>, <em class="sig-param">force=False</em>, <em class="sig-param">quiet=0</em>, <em class="sig-param">legacy=False</em>, <em class="sig-param">optimize=-1</em>, <em class="sig-param">invalidation_mode=None</em><span class="sig-paren">)</span><a class="headerlink" href="#compileall.compile_path" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将在 <code class="docutils literal notranslate"><span class="pre">sys.path</span></code> 中找到的所有 <code class="file docutils literal notranslate"><span class="pre">.py</span></code> 文件编译为字节码。 如果所有文件编译成功则返回真值,否则返回假值。</p>
|
||
<p>如果 <em>skip_curdir</em> 为真值(默认),则当前目录不会被包括在搜索中。 所有其他形参将被传递给 <a class="reference internal" href="#compileall.compile_dir" title="compileall.compile_dir"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile_dir()</span></code></a> 函数。 请注意不同于其他编译函数,<code class="docutils literal notranslate"><span class="pre">maxlevels</span></code> 默认为 <code class="docutils literal notranslate"><span class="pre">0</span></code>。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.2 版更改: </span>增加了 <em>legacy</em> 和 <em>optimize</em> 形参。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.5 版更改: </span><em>quiet</em> 形参已改为多级别值。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.5 版更改: </span><em>legacy</em> 形参将只写入 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 文件而非 <code class="docutils literal notranslate"><span class="pre">.pyo</span></code> 文件,无论 <em>optimize</em> 的值是什么。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.7 版更改: </span>增加了 <em>invalidation_mode</em> 形参。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.7.2 版更改: </span><em>invalidation_mode</em> 形参的默认值更新为 None。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<p>强制重新编译 <code class="file docutils literal notranslate"><span class="pre">Lib/</span></code> 子目录及其所有子目录下的全部 <code class="file docutils literal notranslate"><span class="pre">.py</span></code> 文件:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">compileall</span>
|
||
|
||
<span class="n">compileall</span><span class="o">.</span><span class="n">compile_dir</span><span class="p">(</span><span class="s1">'Lib/'</span><span class="p">,</span> <span class="n">force</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
|
||
|
||
<span class="c1"># Perform same compilation, excluding files in .svn directories.</span>
|
||
<span class="kn">import</span> <span class="nn">re</span>
|
||
<span class="n">compileall</span><span class="o">.</span><span class="n">compile_dir</span><span class="p">(</span><span class="s1">'Lib/'</span><span class="p">,</span> <span class="n">rx</span><span class="o">=</span><span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="sa">r</span><span class="s1">'[/</span><span class="se">\\</span><span class="s1">][.]svn'</span><span class="p">),</span> <span class="n">force</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
|
||
|
||
<span class="c1"># pathlib.Path objects can also be used.</span>
|
||
<span class="kn">import</span> <span class="nn">pathlib</span>
|
||
<span class="n">compileall</span><span class="o">.</span><span class="n">compile_dir</span><span class="p">(</span><span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="s1">'Lib/'</span><span class="p">),</span> <span class="n">force</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="admonition seealso">
|
||
<p class="admonition-title">参见</p>
|
||
<dl class="simple">
|
||
<dt>模块 <a class="reference internal" href="py_compile.html#module-py_compile" title="py_compile: Generate byte-code files from Python source files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">py_compile</span></code></a></dt><dd><p>将单个源文件编译为字节码。</p>
|
||
</dd>
|
||
</dl>
|
||
</div>
|
||
</section>
|
||
</section>
|
||
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
||
<div class="sphinxsidebarwrapper">
|
||
<h3><a href="../contents.html">目录</a></h3>
|
||
<ul>
|
||
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">compileall</span></code> --- 字节编译 Python 库</a><ul>
|
||
<li><a class="reference internal" href="#command-line-use">使用命令行</a></li>
|
||
<li><a class="reference internal" href="#public-functions">公有函数</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="py_compile.html"
|
||
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">py_compile</span></code> --- 编译 Python 源文件</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="dis.html"
|
||
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dis</span></code> --- Python 字节码反汇编器</a></p>
|
||
<div role="note" aria-label="source link">
|
||
<h3>本页</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">报告 Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/compileall.rst"
|
||
rel="nofollow">显示源代码
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="clearer"></div>
|
||
</div>
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>导航</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="总目录"
|
||
>索引</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python 模块索引"
|
||
>模块</a> |</li>
|
||
<li class="right" >
|
||
<a href="dis.html" title="dis --- Python 字节码反汇编器"
|
||
>下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="py_compile.html" title="py_compile --- 编译 Python 源文件"
|
||
>上一页</a> |</li>
|
||
|
||
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
|
||
<li><a href="https://www.python.org/">Python</a> »</li>
|
||
<li class="switchers">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<div class="version_switcher_placeholder"></div>
|
||
</li>
|
||
<li>
|
||
|
||
</li>
|
||
<li id="cpython-language-and-version">
|
||
<a href="../index.html">3.8.20 Documentation</a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> »</li>
|
||
<li class="nav-item nav-item-2"><a href="language.html" >Python 语言服务</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
|
||
<input type="submit" value="转向" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
<div class="footer">
|
||
© <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
|
||
<br />
|
||
This page is licensed under the Python Software Foundation License Version 2.
|
||
<br />
|
||
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
|
||
<br />
|
||
|
||
<br />
|
||
|
||
The Python Software Foundation is a non-profit corporation.
|
||
<a href="https://www.python.org/psf/donations/">Please donate.</a>
|
||
<br />
|
||
<br />
|
||
|
||
最后更新于 12月 09, 2024.
|
||
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
|
||
<br />
|
||
|
||
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
|
||
</div>
|
||
|
||
</body>
|
||
</html> |