1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>32.11. compileall — Byte-compile Python libraries — Python 3.5.2 documentation</title>
11
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
23
<script type="text/javascript" src="../_static/jquery.js"></script>
24
<script type="text/javascript" src="../_static/underscore.js"></script>
25
<script type="text/javascript" src="../_static/doctools.js"></script>
26
<script type="text/javascript" src="../_static/sidebar.js"></script>
27
<link rel="search" type="application/opensearchdescription+xml"
28
title="Search within Python 3.5.2 documentation"
29
href="../_static/opensearch.xml"/>
30
<link rel="author" title="About these documents" href="../about.html" />
31
<link rel="copyright" title="Copyright" href="../copyright.html" />
32
<link rel="top" title="Python 3.5.2 documentation" href="../contents.html" />
33
<link rel="up" title="32. Python Language Services" href="language.html" />
34
<link rel="next" title="32.12. dis — Disassembler for Python bytecode" href="dis.html" />
35
<link rel="prev" title="32.10. py_compile — Compile Python source files" href="py_compile.html" />
36
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
38
<script type="text/javascript" src="../_static/copybutton.js"></script>
39
<script type="text/javascript" src="../_static/version_switch.js"></script>
45
<body role="document">
46
<div class="related" role="navigation" aria-label="related navigation">
49
<li class="right" style="margin-right: 10px">
50
<a href="../genindex.html" title="General Index"
51
accesskey="I">index</a></li>
53
<a href="../py-modindex.html" title="Python Module Index"
56
<a href="dis.html" title="32.12. dis — Disassembler for Python bytecode"
57
accesskey="N">next</a> |</li>
59
<a href="py_compile.html" title="32.10. py_compile — Compile Python source files"
60
accesskey="P">previous</a> |</li>
61
<li><img src="../_static/py.png" alt=""
62
style="vertical-align: middle; margin-top: -1px"/></li>
63
<li><a href="https://www.python.org/">Python</a> »</li>
65
<span class="version_switcher_placeholder">3.5.2</span>
66
<a href="../index.html">Documentation </a> »
69
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
70
<li class="nav-item nav-item-2"><a href="language.html" accesskey="U">32. Python Language Services</a> »</li>
74
<div class="inline-search" style="display: none" role="search">
75
<form class="inline-search" action="../search.html" method="get">
76
<input placeholder="Quick search" type="text" name="q" />
77
<input type="submit" value="Go" />
78
<input type="hidden" name="check_keywords" value="yes" />
79
<input type="hidden" name="area" value="default" />
82
<script type="text/javascript">$('.inline-search').show(0);</script>
89
<div class="document">
90
<div class="documentwrapper">
91
<div class="bodywrapper">
92
<div class="body" role="main">
94
<div class="section" id="module-compileall">
95
<span id="compileall-byte-compile-python-libraries"></span><h1>32.11. <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"><span class="pre">compileall</span></code></a> — Byte-compile Python libraries<a class="headerlink" href="#module-compileall" title="Permalink to this headline">¶</a></h1>
96
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.5/Lib/compileall.py">Lib/compileall.py</a></p>
97
<hr class="docutils" />
98
<p>This module provides some utility functions to support installing Python
99
libraries. These functions compile Python source files in a directory tree.
100
This module can be used to create the cached byte-code files at library
101
installation time, which makes them available for use even by users who don’t
102
have write permission to the library directories.</p>
103
<div class="section" id="command-line-use">
104
<h2>32.11.1. Command-line use<a class="headerlink" href="#command-line-use" title="Permalink to this headline">¶</a></h2>
105
<p>This module can work as a script (using <strong class="program">python -m compileall</strong>) to
106
compile Python sources.</p>
107
<dl class="cmdoption">
108
<dt id="cmdoption-compileall-arg-directory">
109
<code class="descname">directory</code><code class="descclassname"> ...</code><a class="headerlink" href="#cmdoption-compileall-arg-directory" title="Permalink to this definition">¶</a></dt>
110
<dt id="cmdoption-compileall-arg-file">
111
<code class="descname">file</code><code class="descclassname"> ...</code><a class="headerlink" href="#cmdoption-compileall-arg-file" title="Permalink to this definition">¶</a></dt>
112
<dd><p>Positional arguments are files to compile or directories that contain
113
source files, traversed recursively. If no argument is given, behave as if
114
the command line was <code class="docutils literal"><span class="pre">-l</span> <span class="pre"><directories</span> <span class="pre">from</span> <span class="pre">sys.path></span></code>.</p>
117
<dl class="cmdoption">
118
<dt id="cmdoption-compileall-l">
119
<code class="descname">-l</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-compileall-l" title="Permalink to this definition">¶</a></dt>
120
<dd><p>Do not recurse into subdirectories, only compile source code files directly
121
contained in the named or implied directories.</p>
124
<dl class="cmdoption">
125
<dt id="cmdoption-compileall-f">
126
<code class="descname">-f</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-compileall-f" title="Permalink to this definition">¶</a></dt>
127
<dd><p>Force rebuild even if timestamps are up-to-date.</p>
130
<dl class="cmdoption">
131
<dt id="cmdoption-compileall-q">
132
<code class="descname">-q</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-compileall-q" title="Permalink to this definition">¶</a></dt>
133
<dd><p>Do not print the list of files compiled. If passed once, error messages will
134
still be printed. If passed twice (<code class="docutils literal"><span class="pre">-qq</span></code>), all output is suppressed.</p>
137
<dl class="cmdoption">
138
<dt id="cmdoption-compileall-d">
139
<code class="descname">-d</code><code class="descclassname"> destdir</code><a class="headerlink" href="#cmdoption-compileall-d" title="Permalink to this definition">¶</a></dt>
140
<dd><p>Directory prepended to the path to each file being compiled. This will
141
appear in compilation time tracebacks, and is also compiled in to the
142
byte-code file, where it will be used in tracebacks and other messages in
143
cases where the source file does not exist at the time the byte-code file is
147
<dl class="cmdoption">
148
<dt id="cmdoption-compileall-x">
149
<code class="descname">-x</code><code class="descclassname"> regex</code><a class="headerlink" href="#cmdoption-compileall-x" title="Permalink to this definition">¶</a></dt>
150
<dd><p>regex is used to search the full path to each file considered for
151
compilation, and if the regex produces a match, the file is skipped.</p>
154
<dl class="cmdoption">
155
<dt id="cmdoption-compileall-i">
156
<code class="descname">-i</code><code class="descclassname"> list</code><a class="headerlink" href="#cmdoption-compileall-i" title="Permalink to this definition">¶</a></dt>
157
<dd><p>Read the file <code class="docutils literal"><span class="pre">list</span></code> and add each line that it contains to the list of
158
files and directories to compile. If <code class="docutils literal"><span class="pre">list</span></code> is <code class="docutils literal"><span class="pre">-</span></code>, read lines from
159
<code class="docutils literal"><span class="pre">stdin</span></code>.</p>
162
<dl class="cmdoption">
163
<dt id="cmdoption-compileall-b">
164
<code class="descname">-b</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-compileall-b" title="Permalink to this definition">¶</a></dt>
165
<dd><p>Write the byte-code files to their legacy locations and names, which may
166
overwrite byte-code files created by another version of Python. The default
167
is to write files to their <span class="target" id="index-0"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3147"><strong>PEP 3147</strong></a> locations and names, which allows
168
byte-code files from multiple versions of Python to coexist.</p>
171
<dl class="cmdoption">
172
<dt id="cmdoption-compileall-r">
173
<code class="descname">-r</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-compileall-r" title="Permalink to this definition">¶</a></dt>
174
<dd><p>Control the maximum recursion level for subdirectories.
175
If this is given, then <code class="docutils literal"><span class="pre">-l</span></code> option will not be taken into account.
176
<strong class="program">python -m compileall <directory> -r 0</strong> is equivalent to
177
<strong class="program">python -m compileall <directory> -l</strong>.</p>
180
<dl class="cmdoption">
181
<dt id="cmdoption-compileall-j">
182
<code class="descname">-j</code><code class="descclassname"> N</code><a class="headerlink" href="#cmdoption-compileall-j" title="Permalink to this definition">¶</a></dt>
183
<dd><p>Use <em>N</em> workers to compile the files within the given directory.
184
If <code class="docutils literal"><span class="pre">0</span></code> is used, then the result of <a class="reference internal" href="os.html#os.cpu_count" title="os.cpu_count"><code class="xref py py-func docutils literal"><span class="pre">os.cpu_count()</span></code></a>
188
<div class="versionchanged">
189
<p><span class="versionmodified">Changed in version 3.2: </span>Added the <code class="docutils literal"><span class="pre">-i</span></code>, <code class="docutils literal"><span class="pre">-b</span></code> and <code class="docutils literal"><span class="pre">-h</span></code> options.</p>
191
<div class="versionchanged">
192
<p><span class="versionmodified">Changed in version 3.5: </span>Added the <code class="docutils literal"><span class="pre">-j</span></code>, <code class="docutils literal"><span class="pre">-r</span></code>, and <code class="docutils literal"><span class="pre">-qq</span></code> options. <code class="docutils literal"><span class="pre">-q</span></code> option
193
was changed to a multilevel value. <code class="docutils literal"><span class="pre">-b</span></code> will always produce a
194
byte-code file ending in <code class="docutils literal"><span class="pre">.pyc</span></code>, never <code class="docutils literal"><span class="pre">.pyo</span></code>.</p>
196
<p>There is no command-line option to control the optimization level used by the
197
<a class="reference internal" href="functions.html#compile" title="compile"><code class="xref py py-func docutils literal"><span class="pre">compile()</span></code></a> function, because the Python interpreter itself already
198
provides the option: <strong class="program">python -O -m compileall</strong>.</p>
200
<div class="section" id="public-functions">
201
<h2>32.11.2. Public functions<a class="headerlink" href="#public-functions" title="Permalink to this headline">¶</a></h2>
202
<dl class="function">
203
<dt id="compileall.compile_dir">
204
<code class="descclassname">compileall.</code><code class="descname">compile_dir</code><span class="sig-paren">(</span><em>dir</em>, <em>maxlevels=10</em>, <em>ddir=None</em>, <em>force=False</em>, <em>rx=None</em>, <em>quiet=0</em>, <em>legacy=False</em>, <em>optimize=-1</em>, <em>workers=1</em><span class="sig-paren">)</span><a class="headerlink" href="#compileall.compile_dir" title="Permalink to this definition">¶</a></dt>
205
<dd><p>Recursively descend the directory tree named by <em>dir</em>, compiling all <code class="file docutils literal"><span class="pre">.py</span></code>
206
files along the way.</p>
207
<p>The <em>maxlevels</em> parameter is used to limit the depth of the recursion; it
208
defaults to <code class="docutils literal"><span class="pre">10</span></code>.</p>
209
<p>If <em>ddir</em> is given, it is prepended to the path to each file being compiled
210
for use in compilation time tracebacks, and is also compiled in to the
211
byte-code file, where it will be used in tracebacks and other messages in
212
cases where the source file does not exist at the time the byte-code file is
214
<p>If <em>force</em> is true, modules are re-compiled even if the timestamps are up to
216
<p>If <em>rx</em> is given, its search method is called on the complete path to each
217
file considered for compilation, and if it returns a true value, the file
219
<p>If <em>quiet</em> is <code class="docutils literal"><span class="pre">False</span></code> or <code class="docutils literal"><span class="pre">0</span></code> (the default), the filenames and other
220
information are printed to standard out. Set to <code class="docutils literal"><span class="pre">1</span></code>, only errors are
221
printed. Set to <code class="docutils literal"><span class="pre">2</span></code>, all output is suppressed.</p>
222
<p>If <em>legacy</em> is true, byte-code files are written to their legacy locations
223
and names, which may overwrite byte-code files created by another version of
224
Python. The default is to write files to their <span class="target" id="index-1"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3147"><strong>PEP 3147</strong></a> locations and
225
names, which allows byte-code files from multiple versions of Python to
227
<p><em>optimize</em> specifies the optimization level for the compiler. It is passed to
228
the built-in <a class="reference internal" href="functions.html#compile" title="compile"><code class="xref py py-func docutils literal"><span class="pre">compile()</span></code></a> function.</p>
229
<p>The argument <em>workers</em> specifies how many workers are used to
230
compile files in parallel. The default is to not use multiple workers.
231
If the platform can’t use multiple workers and <em>workers</em> argument is given,
232
then sequential compilation will be used as a fallback. If <em>workers</em> is
233
lower than <code class="docutils literal"><span class="pre">0</span></code>, a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a> will be raised.</p>
234
<div class="versionchanged">
235
<p><span class="versionmodified">Changed in version 3.2: </span>Added the <em>legacy</em> and <em>optimize</em> parameter.</p>
237
<div class="versionchanged">
238
<p><span class="versionmodified">Changed in version 3.5: </span>Added the <em>workers</em> parameter.</p>
240
<div class="versionchanged">
241
<p><span class="versionmodified">Changed in version 3.5: </span><em>quiet</em> parameter was changed to a multilevel value.</p>
243
<div class="versionchanged">
244
<p><span class="versionmodified">Changed in version 3.5: </span>The <em>legacy</em> parameter only writes out <code class="docutils literal"><span class="pre">.pyc</span></code> files, not <code class="docutils literal"><span class="pre">.pyo</span></code> files
245
no matter what the value of <em>optimize</em> is.</p>
249
<dl class="function">
250
<dt id="compileall.compile_file">
251
<code class="descclassname">compileall.</code><code class="descname">compile_file</code><span class="sig-paren">(</span><em>fullname</em>, <em>ddir=None</em>, <em>force=False</em>, <em>rx=None</em>, <em>quiet=0</em>, <em>legacy=False</em>, <em>optimize=-1</em><span class="sig-paren">)</span><a class="headerlink" href="#compileall.compile_file" title="Permalink to this definition">¶</a></dt>
252
<dd><p>Compile the file with path <em>fullname</em>.</p>
253
<p>If <em>ddir</em> is given, it is prepended to the path to the file being compiled
254
for use in compilation time tracebacks, and is also compiled in to the
255
byte-code file, where it will be used in tracebacks and other messages in
256
cases where the source file does not exist at the time the byte-code file is
258
<p>If <em>rx</em> is given, its search method is passed the full path name to the
259
file being compiled, and if it returns a true value, the file is not
260
compiled and <code class="docutils literal"><span class="pre">True</span></code> is returned.</p>
261
<p>If <em>quiet</em> is <code class="docutils literal"><span class="pre">False</span></code> or <code class="docutils literal"><span class="pre">0</span></code> (the default), the filenames and other
262
information are printed to standard out. Set to <code class="docutils literal"><span class="pre">1</span></code>, only errors are
263
printed. Set to <code class="docutils literal"><span class="pre">2</span></code>, all output is suppressed.</p>
264
<p>If <em>legacy</em> is true, byte-code files are written to their legacy locations
265
and names, which may overwrite byte-code files created by another version of
266
Python. The default is to write files to their <span class="target" id="index-2"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3147"><strong>PEP 3147</strong></a> locations and
267
names, which allows byte-code files from multiple versions of Python to
269
<p><em>optimize</em> specifies the optimization level for the compiler. It is passed to
270
the built-in <a class="reference internal" href="functions.html#compile" title="compile"><code class="xref py py-func docutils literal"><span class="pre">compile()</span></code></a> function.</p>
271
<div class="versionadded">
272
<p><span class="versionmodified">New in version 3.2.</span></p>
274
<div class="versionchanged">
275
<p><span class="versionmodified">Changed in version 3.5: </span><em>quiet</em> parameter was changed to a multilevel value.</p>
277
<div class="versionchanged">
278
<p><span class="versionmodified">Changed in version 3.5: </span>The <em>legacy</em> parameter only writes out <code class="docutils literal"><span class="pre">.pyc</span></code> files, not <code class="docutils literal"><span class="pre">.pyo</span></code> files
279
no matter what the value of <em>optimize</em> is.</p>
283
<dl class="function">
284
<dt id="compileall.compile_path">
285
<code class="descclassname">compileall.</code><code class="descname">compile_path</code><span class="sig-paren">(</span><em>skip_curdir=True</em>, <em>maxlevels=0</em>, <em>force=False</em>, <em>quiet=0</em>, <em>legacy=False</em>, <em>optimize=-1</em><span class="sig-paren">)</span><a class="headerlink" href="#compileall.compile_path" title="Permalink to this definition">¶</a></dt>
286
<dd><p>Byte-compile all the <code class="file docutils literal"><span class="pre">.py</span></code> files found along <code class="docutils literal"><span class="pre">sys.path</span></code>. If
287
<em>skip_curdir</em> is true (the default), the current directory is not included
288
in the search. All other parameters are passed to the <a class="reference internal" href="#compileall.compile_dir" title="compileall.compile_dir"><code class="xref py py-func docutils literal"><span class="pre">compile_dir()</span></code></a>
289
function. Note that unlike the other compile functions, <code class="docutils literal"><span class="pre">maxlevels</span></code>
290
defaults to <code class="docutils literal"><span class="pre">0</span></code>.</p>
291
<div class="versionchanged">
292
<p><span class="versionmodified">Changed in version 3.2: </span>Added the <em>legacy</em> and <em>optimize</em> parameter.</p>
294
<div class="versionchanged">
295
<p><span class="versionmodified">Changed in version 3.5: </span><em>quiet</em> parameter was changed to a multilevel value.</p>
297
<div class="versionchanged">
298
<p><span class="versionmodified">Changed in version 3.5: </span>The <em>legacy</em> parameter only writes out <code class="docutils literal"><span class="pre">.pyc</span></code> files, not <code class="docutils literal"><span class="pre">.pyo</span></code> files
299
no matter what the value of <em>optimize</em> is.</p>
303
<p>To force a recompile of all the <code class="file docutils literal"><span class="pre">.py</span></code> files in the <code class="file docutils literal"><span class="pre">Lib/</span></code>
304
subdirectory and all its subdirectories:</p>
305
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">compileall</span>
307
<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>
309
<span class="c1"># Perform same compilation, excluding files in .svn directories.</span>
310
<span class="kn">import</span> <span class="nn">re</span>
311
<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="s1">r'[/</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>
314
<div class="admonition seealso">
315
<p class="first admonition-title">See also</p>
316
<dl class="last docutils">
317
<dt>Module <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"><span class="pre">py_compile</span></code></a></dt>
318
<dd>Byte-compile a single source file.</dd>
328
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
329
<div class="sphinxsidebarwrapper">
330
<h3><a href="../contents.html">Table Of Contents</a></h3>
332
<li><a class="reference internal" href="#">32.11. <code class="docutils literal"><span class="pre">compileall</span></code> — Byte-compile Python libraries</a><ul>
333
<li><a class="reference internal" href="#command-line-use">32.11.1. Command-line use</a></li>
334
<li><a class="reference internal" href="#public-functions">32.11.2. Public functions</a></li>
339
<h4>Previous topic</h4>
340
<p class="topless"><a href="py_compile.html"
341
title="previous chapter">32.10. <code class="docutils literal"><span class="pre">py_compile</span></code> — Compile Python source files</a></p>
343
<p class="topless"><a href="dis.html"
344
title="next chapter">32.12. <code class="docutils literal"><span class="pre">dis</span></code> — Disassembler for Python bytecode</a></p>
345
<div role="note" aria-label="source link">
347
<ul class="this-page-menu">
348
<li><a href="../bugs.html">Report a Bug</a></li>
349
<li><a href="../_sources/library/compileall.txt"
350
rel="nofollow">Show Source</a></li>
355
<div class="clearer"></div>
357
<div class="related" role="navigation" aria-label="related navigation">
360
<li class="right" style="margin-right: 10px">
361
<a href="../genindex.html" title="General Index"
364
<a href="../py-modindex.html" title="Python Module Index"
367
<a href="dis.html" title="32.12. dis — Disassembler for Python bytecode"
370
<a href="py_compile.html" title="32.10. py_compile — Compile Python source files"
372
<li><img src="../_static/py.png" alt=""
373
style="vertical-align: middle; margin-top: -1px"/></li>
374
<li><a href="https://www.python.org/">Python</a> »</li>
376
<span class="version_switcher_placeholder">3.5.2</span>
377
<a href="../index.html">Documentation </a> »
380
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
381
<li class="nav-item nav-item-2"><a href="language.html" >32. Python Language Services</a> »</li>
385
<div class="inline-search" style="display: none" role="search">
386
<form class="inline-search" action="../search.html" method="get">
387
<input placeholder="Quick search" type="text" name="q" />
388
<input type="submit" value="Go" />
389
<input type="hidden" name="check_keywords" value="yes" />
390
<input type="hidden" name="area" value="default" />
393
<script type="text/javascript">$('.inline-search').show(0);</script>
400
© <a href="../copyright.html">Copyright</a> 2001-2016, Python Software Foundation.
402
The Python Software Foundation is a non-profit corporation.
403
<a href="https://www.python.org/psf/donations/">Please donate.</a>
405
Last updated on Sep 23, 2016.
406
<a href="../bugs.html">Found a bug</a>?
408
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
b'\\ No newline at end of file'