-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompileall.html
More file actions
448 lines (410 loc) · 30.6 KB
/
compileall.html
File metadata and controls
448 lines (410 loc) · 30.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_TW">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>33.11. compileall — Byte-compile Python libraries — Python 3.7.0 說明文件</title>
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="../_static/translations.js"></script>
<script type="text/javascript" src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="在 Python 3.7.0 說明文件 中搜尋"
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="Copyright" href="../copyright.html" />
<link rel="next" title="33.12. dis — Python bytecode的反組譯器" href="dis.html" />
<link rel="prev" title="33.10. py_compile — Compile Python source files" href="py_compile.html" />
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
<link rel="canonical" href="https://docs.python.org/3/library/compileall.html" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/switchers.js"></script>
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>瀏覽</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模組索引"
>模組</a> |</li>
<li class="right" >
<a href="dis.html" title="33.12. dis — Python bytecode的反組譯器"
accesskey="N">下一頁</a> |</li>
<li class="right" >
<a href="py_compile.html" title="33.10. py_compile — Compile Python source files"
accesskey="P">上一頁</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li>
<span class="language_switcher_placeholder">zh_TW</span>
<span class="version_switcher_placeholder">3.7.0</span>
<a href="../index.html">Documentation </a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 標準函式庫 (Standard Library)</a> »</li>
<li class="nav-item nav-item-2"><a href="language.html" accesskey="U">33. Python Language Services</a> »</li>
<li class="right">
<div class="inline-search" style="display: none" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('.inline-search').show(0);</script>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="module-compileall">
<span id="compileall-byte-compile-python-libraries"></span><h1>33.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 notranslate"><span class="pre">compileall</span></code></a> — Byte-compile Python libraries<a class="headerlink" href="#module-compileall" title="本標題的永久連結">¶</a></h1>
<p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.7/Lib/compileall.py">Lib/compileall.py</a></p>
<hr class="docutils" />
<p>This module provides some utility functions to support installing Python
libraries. These functions compile Python source files in a directory tree.
This module can be used to create the cached byte-code files at library
installation time, which makes them available for use even by users who don’t
have write permission to the library directories.</p>
<div class="section" id="command-line-use">
<h2>33.11.1. Command-line use<a class="headerlink" href="#command-line-use" title="本標題的永久連結">¶</a></h2>
<p>This module can work as a script (using <strong class="program">python -m compileall</strong>) to
compile Python sources.</p>
<dl class="cmdoption">
<dt id="cmdoption-compileall-arg-directory">
<code class="descname">directory</code><code class="descclassname"> ...</code><a class="headerlink" href="#cmdoption-compileall-arg-directory" title="本定義的永久連結">¶</a></dt>
<dt id="cmdoption-compileall-arg-file">
<code class="descname">file</code><code class="descclassname"> ...</code><a class="headerlink" href="#cmdoption-compileall-arg-file" title="本定義的永久連結">¶</a></dt>
<dd><p>Positional arguments are files to compile or directories that contain
source files, traversed recursively. If no argument is given, behave as if
the command line was <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="descname">-l</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-compileall-l" title="本定義的永久連結">¶</a></dt>
<dd><p>Do not recurse into subdirectories, only compile source code files directly
contained in the named or implied directories.</p>
</dd></dl>
<dl class="cmdoption">
<dt id="cmdoption-compileall-f">
<code class="descname">-f</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-compileall-f" title="本定義的永久連結">¶</a></dt>
<dd><p>Force rebuild even if timestamps are up-to-date.</p>
</dd></dl>
<dl class="cmdoption">
<dt id="cmdoption-compileall-q">
<code class="descname">-q</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-compileall-q" title="本定義的永久連結">¶</a></dt>
<dd><p>Do not print the list of files compiled. If passed once, error messages will
still be printed. If passed twice (<code class="docutils literal notranslate"><span class="pre">-qq</span></code>), all output is suppressed.</p>
</dd></dl>
<dl class="cmdoption">
<dt id="cmdoption-compileall-d">
<code class="descname">-d</code><code class="descclassname"> destdir</code><a class="headerlink" href="#cmdoption-compileall-d" title="本定義的永久連結">¶</a></dt>
<dd><p>Directory prepended to the path to each file being compiled. This will
appear in compilation time tracebacks, and is also compiled in to the
byte-code file, where it will be used in tracebacks and other messages in
cases where the source file does not exist at the time the byte-code file is
executed.</p>
</dd></dl>
<dl class="cmdoption">
<dt id="cmdoption-compileall-x">
<code class="descname">-x</code><code class="descclassname"> regex</code><a class="headerlink" href="#cmdoption-compileall-x" title="本定義的永久連結">¶</a></dt>
<dd><p>regex is used to search the full path to each file considered for
compilation, and if the regex produces a match, the file is skipped.</p>
</dd></dl>
<dl class="cmdoption">
<dt id="cmdoption-compileall-i">
<code class="descname">-i</code><code class="descclassname"> list</code><a class="headerlink" href="#cmdoption-compileall-i" title="本定義的永久連結">¶</a></dt>
<dd><p>Read the file <code class="docutils literal notranslate"><span class="pre">list</span></code> and add each line that it contains to the list of
files and directories to compile. If <code class="docutils literal notranslate"><span class="pre">list</span></code> is <code class="docutils literal notranslate"><span class="pre">-</span></code>, read lines from
<code class="docutils literal notranslate"><span class="pre">stdin</span></code>.</p>
</dd></dl>
<dl class="cmdoption">
<dt id="cmdoption-compileall-b">
<code class="descname">-b</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-compileall-b" title="本定義的永久連結">¶</a></dt>
<dd><p>Write the byte-code files to their legacy locations and names, which may
overwrite byte-code files created by another version of Python. The default
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
byte-code files from multiple versions of Python to coexist.</p>
</dd></dl>
<dl class="cmdoption">
<dt id="cmdoption-compileall-r">
<code class="descname">-r</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-compileall-r" title="本定義的永久連結">¶</a></dt>
<dd><p>Control the maximum recursion level for subdirectories.
If this is given, then <code class="docutils literal notranslate"><span class="pre">-l</span></code> option will not be taken into account.
<strong class="program">python -m compileall <directory> -r 0</strong> is equivalent to
<strong class="program">python -m compileall <directory> -l</strong>.</p>
</dd></dl>
<dl class="cmdoption">
<dt id="cmdoption-compileall-j">
<code class="descname">-j</code><code class="descclassname"> N</code><a class="headerlink" href="#cmdoption-compileall-j" title="本定義的永久連結">¶</a></dt>
<dd><p>Use <em>N</em> workers to compile the files within the given directory.
If <code class="docutils literal notranslate"><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 notranslate"><span class="pre">os.cpu_count()</span></code></a>
will be used.</p>
</dd></dl>
<dl class="cmdoption">
<dt id="cmdoption-compileall-invalidation-mode">
<code class="descname">--invalidation-mode</code><code class="descclassname"> [timestamp|checked-hash|unchecked-hash]</code><a class="headerlink" href="#cmdoption-compileall-invalidation-mode" title="本定義的永久連結">¶</a></dt>
<dd><p>Control how the generated pycs will be invalidated at runtime. The default
setting, <code class="docutils literal notranslate"><span class="pre">timestamp</span></code>, means that <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> files with the source timestamp
and size embedded will be generated. The <code class="docutils literal notranslate"><span class="pre">checked-hash</span></code> and
<code class="docutils literal notranslate"><span class="pre">unchecked-hash</span></code> values cause hash-based pycs to be generated. Hash-based
pycs embed a hash of the source file contents rather than a timestamp. See
<a class="reference internal" href="../reference/import.html#pyc-invalidation"><span class="std std-ref">Cached bytecode invalidation</span></a> for more information on how Python validates bytecode
cache files at runtime.</p>
</dd></dl>
<div class="versionchanged">
<p><span class="versionmodified">3.2 版更變: </span>Added the <code class="docutils literal notranslate"><span class="pre">-i</span></code>, <code class="docutils literal notranslate"><span class="pre">-b</span></code> and <code class="docutils literal notranslate"><span class="pre">-h</span></code> options.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">3.5 版更變: </span>Added the <code class="docutils literal notranslate"><span class="pre">-j</span></code>, <code class="docutils literal notranslate"><span class="pre">-r</span></code>, and <code class="docutils literal notranslate"><span class="pre">-qq</span></code> options. <code class="docutils literal notranslate"><span class="pre">-q</span></code> option
was changed to a multilevel value. <code class="docutils literal notranslate"><span class="pre">-b</span></code> will always produce a
byte-code file ending in <code class="docutils literal notranslate"><span class="pre">.pyc</span></code>, never <code class="docutils literal notranslate"><span class="pre">.pyo</span></code>.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">3.7 版更變: </span>Added the <code class="docutils literal notranslate"><span class="pre">--invalidation-mode</span></code> parameter.</p>
</div>
<p>There is no command-line option to control the optimization level used by the
<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, because the Python interpreter itself already
provides the option: <strong class="program">python -O -m compileall</strong>.</p>
</div>
<div class="section" id="public-functions">
<h2>33.11.2. Public functions<a class="headerlink" href="#public-functions" title="本標題的永久連結">¶</a></h2>
<dl class="function">
<dt id="compileall.compile_dir">
<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>, <em>invalidation_mode=py_compile.PycInvalidationMode.TIMESTAMP</em><span class="sig-paren">)</span><a class="headerlink" href="#compileall.compile_dir" title="本定義的永久連結">¶</a></dt>
<dd><p>Recursively descend the directory tree named by <em>dir</em>, compiling all <code class="file docutils literal notranslate"><span class="pre">.py</span></code>
files along the way. Return a true value if all the files compiled successfully,
and a false value otherwise.</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>If <em>ddir</em> is given, it is prepended to the path to each file being compiled
for use in compilation time tracebacks, and is also compiled in to the
byte-code file, where it will be used in tracebacks and other messages in
cases where the source file does not exist at the time the byte-code file is
executed.</p>
<p>If <em>force</em> is true, modules are re-compiled even if the timestamps are up to
date.</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>If <em>quiet</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code> or <code class="docutils literal notranslate"><span class="pre">0</span></code> (the default), the filenames and other
information are printed to standard out. Set to <code class="docutils literal notranslate"><span class="pre">1</span></code>, only errors are
printed. Set to <code class="docutils literal notranslate"><span class="pre">2</span></code>, all output is suppressed.</p>
<p>If <em>legacy</em> is true, byte-code files are written to their legacy locations
and names, which may overwrite byte-code files created by another version of
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
names, which allows byte-code files from multiple versions of Python to
coexist.</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>The argument <em>workers</em> specifies how many workers are used to
compile files in parallel. The default is to not use multiple workers.
If the platform can’t use multiple workers and <em>workers</em> argument is given,
then sequential compilation will be used as a fallback. If <em>workers</em> is
lower than <code class="docutils literal notranslate"><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 notranslate"><span class="pre">ValueError</span></code></a> will be raised.</p>
<p><em>invalidation_mode</em> should be a member of the
<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> enum and controls how the generated
pycs are invalidated at runtime.</p>
<div class="versionchanged">
<p><span class="versionmodified">3.2 版更變: </span>Added the <em>legacy</em> and <em>optimize</em> parameter.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">3.5 版更變: </span>Added the <em>workers</em> parameter.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">3.5 版更變: </span><em>quiet</em> parameter was changed to a multilevel value.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">3.5 版更變: </span>The <em>legacy</em> parameter only writes out <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> files, not <code class="docutils literal notranslate"><span class="pre">.pyo</span></code> files
no matter what the value of <em>optimize</em> is.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">3.6 版更變: </span>Accepts a <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">3.7 版更變: </span>The <em>invalidation_mode</em> parameter was added.</p>
</div>
</dd></dl>
<dl class="function">
<dt id="compileall.compile_file">
<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>, <em>invalidation_mode=py_compile.PycInvalidationMode.TIMESTAMP</em><span class="sig-paren">)</span><a class="headerlink" href="#compileall.compile_file" title="本定義的永久連結">¶</a></dt>
<dd><p>Compile the file with path <em>fullname</em>. Return a true value if the file
compiled successfully, and a false value otherwise.</p>
<p>If <em>ddir</em> is given, it is prepended to the path to the file being compiled
for use in compilation time tracebacks, and is also compiled in to the
byte-code file, where it will be used in tracebacks and other messages in
cases where the source file does not exist at the time the byte-code file is
executed.</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>If <em>quiet</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code> or <code class="docutils literal notranslate"><span class="pre">0</span></code> (the default), the filenames and other
information are printed to standard out. Set to <code class="docutils literal notranslate"><span class="pre">1</span></code>, only errors are
printed. Set to <code class="docutils literal notranslate"><span class="pre">2</span></code>, all output is suppressed.</p>
<p>If <em>legacy</em> is true, byte-code files are written to their legacy locations
and names, which may overwrite byte-code files created by another version of
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
names, which allows byte-code files from multiple versions of Python to
coexist.</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> should be a member of the
<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> enum and controls how the generated
pycs are invalidated at runtime.</p>
<div class="versionadded">
<p><span class="versionmodified">3.2 版新加入.</span></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">3.5 版更變: </span><em>quiet</em> parameter was changed to a multilevel value.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">3.5 版更變: </span>The <em>legacy</em> parameter only writes out <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> files, not <code class="docutils literal notranslate"><span class="pre">.pyo</span></code> files
no matter what the value of <em>optimize</em> is.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">3.7 版更變: </span>The <em>invalidation_mode</em> parameter was added.</p>
</div>
</dd></dl>
<dl class="function">
<dt id="compileall.compile_path">
<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>, <em>invalidation_mode=py_compile.PycInvalidationMode.TIMESTAMP</em><span class="sig-paren">)</span><a class="headerlink" href="#compileall.compile_path" title="本定義的永久連結">¶</a></dt>
<dd><p>Byte-compile all the <code class="file docutils literal notranslate"><span class="pre">.py</span></code> files found along <code class="docutils literal notranslate"><span class="pre">sys.path</span></code>. Return a
true value if all the files compiled successfully, and a false value otherwise.</p>
<p>If <em>skip_curdir</em> is true (the default), the current directory is not included
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 notranslate"><span class="pre">compile_dir()</span></code></a>
function. Note that unlike the other compile functions, <code class="docutils literal notranslate"><span class="pre">maxlevels</span></code>
defaults to <code class="docutils literal notranslate"><span class="pre">0</span></code>.</p>
<div class="versionchanged">
<p><span class="versionmodified">3.2 版更變: </span>Added the <em>legacy</em> and <em>optimize</em> parameter.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">3.5 版更變: </span><em>quiet</em> parameter was changed to a multilevel value.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">3.5 版更變: </span>The <em>legacy</em> parameter only writes out <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> files, not <code class="docutils literal notranslate"><span class="pre">.pyo</span></code> files
no matter what the value of <em>optimize</em> is.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">3.7 版更變: </span>The <em>invalidation_mode</em> parameter was added.</p>
</div>
</dd></dl>
<p>To force a recompile of all the <code class="file docutils literal notranslate"><span class="pre">.py</span></code> files in the <code class="file docutils literal notranslate"><span class="pre">Lib/</span></code>
subdirectory and all its subdirectories:</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="first admonition-title">也參考</p>
<dl class="last docutils">
<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 notranslate"><span class="pre">py_compile</span></code></a></dt>
<dd>Byte-compile a single source file.</dd>
</dl>
</div>
</div>
</div>
</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="#">33.11. <code class="docutils literal notranslate"><span class="pre">compileall</span></code> — Byte-compile Python libraries</a><ul>
<li><a class="reference internal" href="#command-line-use">33.11.1. Command-line use</a></li>
<li><a class="reference internal" href="#public-functions">33.11.2. Public functions</a></li>
</ul>
</li>
</ul>
<h4>上個主題</h4>
<p class="topless"><a href="py_compile.html"
title="上一章">33.10. <code class="docutils literal notranslate"><span class="pre">py_compile</span></code> — Compile Python source files</a></p>
<h4>下個主題</h4>
<p class="topless"><a href="dis.html"
title="下一章">33.12. <code class="docutils literal notranslate"><span class="pre">dis</span></code> — Python bytecode的反組譯器</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">Report a Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.7/Doc/library/compileall.rst"
rel="nofollow">Show Source
</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="General Index"
>索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模組索引"
>模組</a> |</li>
<li class="right" >
<a href="dis.html" title="33.12. dis — Python bytecode的反組譯器"
>下一頁</a> |</li>
<li class="right" >
<a href="py_compile.html" title="33.10. py_compile — Compile Python source files"
>上一頁</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li>
<span class="language_switcher_placeholder">zh_TW</span>
<span class="version_switcher_placeholder">3.7.0</span>
<a href="../index.html">Documentation </a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 標準函式庫 (Standard Library)</a> »</li>
<li class="nav-item nav-item-2"><a href="language.html" >33. Python Language Services</a> »</li>
<li class="right">
<div class="inline-search" style="display: none" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('.inline-search').show(0);</script>
|
</li>
</ul>
</div>
<div class="footer">
© <a href="../copyright.html">Copyright</a> 2001-2018, Python Software Foundation.
<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
Last updated on 8月 22, 2018.
<a href="../bugs.html">Found a bug</a>?
<br />
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.7.7.
</div>
</body>
</html>