blob: 7b3963da894fbaa850d642c196f25fa58fb5ab5c [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`compileall` --- Byte-compile Python libraries
2===================================================
3
4.. module:: compileall
5 :synopsis: Tools for byte-compiling all Python source files in a directory tree.
6
Georg Brandlc0a8f8c2014-10-02 08:38:39 +02007**Source code:** :source:`Lib/compileall.py`
8
9--------------
10
Georg Brandl116aa622007-08-15 14:28:22 +000011This module provides some utility functions to support installing Python
R. David Murray94f58c32010-12-17 16:29:07 +000012libraries. These functions compile Python source files in a directory tree.
13This module can be used to create the cached byte-code files at library
14installation time, which makes them available for use even by users who don't
15have write permission to the library directories.
Georg Brandl116aa622007-08-15 14:28:22 +000016
Georg Brandl116aa622007-08-15 14:28:22 +000017
Éric Araujo713d3032010-11-18 16:38:46 +000018Command-line use
19----------------
20
21This module can work as a script (using :program:`python -m compileall`) to
22compile Python sources.
23
24.. program:: compileall
25
Georg Brandlf5c801f2014-03-18 07:44:07 +010026.. cmdoption:: directory ...
27 file ...
Éric Araujo713d3032010-11-18 16:38:46 +000028
29 Positional arguments are files to compile or directories that contain
30 source files, traversed recursively. If no argument is given, behave as if
31 the command line was ``-l <directories from sys.path>``.
32
33.. cmdoption:: -l
34
R. David Murray94f58c32010-12-17 16:29:07 +000035 Do not recurse into subdirectories, only compile source code files directly
36 contained in the named or implied directories.
Éric Araujo713d3032010-11-18 16:38:46 +000037
38.. cmdoption:: -f
39
40 Force rebuild even if timestamps are up-to-date.
41
42.. cmdoption:: -q
43
Berker Peksag6554b862014-10-15 11:10:57 +030044 Do not print the list of files compiled. If passed once, error messages will
45 still be printed. If passed twice (``-qq``), all output is suppressed.
Éric Araujo713d3032010-11-18 16:38:46 +000046
47.. cmdoption:: -d destdir
48
R. David Murray94f58c32010-12-17 16:29:07 +000049 Directory prepended to the path to each file being compiled. This will
50 appear in compilation time tracebacks, and is also compiled in to the
51 byte-code file, where it will be used in tracebacks and other messages in
52 cases where the source file does not exist at the time the byte-code file is
53 executed.
Éric Araujo713d3032010-11-18 16:38:46 +000054
55.. cmdoption:: -x regex
56
R. David Murray94f58c32010-12-17 16:29:07 +000057 regex is used to search the full path to each file considered for
58 compilation, and if the regex produces a match, the file is skipped.
Éric Araujo713d3032010-11-18 16:38:46 +000059
60.. cmdoption:: -i list
61
R. David Murray94f58c32010-12-17 16:29:07 +000062 Read the file ``list`` and add each line that it contains to the list of
63 files and directories to compile. If ``list`` is ``-``, read lines from
64 ``stdin``.
Éric Araujo713d3032010-11-18 16:38:46 +000065
66.. cmdoption:: -b
67
R. David Murray94f58c32010-12-17 16:29:07 +000068 Write the byte-code files to their legacy locations and names, which may
69 overwrite byte-code files created by another version of Python. The default
70 is to write files to their :pep:`3147` locations and names, which allows
71 byte-code files from multiple versions of Python to coexist.
Éric Araujo713d3032010-11-18 16:38:46 +000072
Benjamin Peterson344ff4a2014-08-19 16:13:26 -050073.. cmdoption:: -r
74
75 Control the maximum recursion level for subdirectories.
76 If this is given, then ``-l`` option will not be taken into account.
77 :program:`python -m compileall <directory> -r 0` is equivalent to
78 :program:`python -m compileall <directory> -l`.
79
Brett Cannonf1a8df02014-09-12 10:39:48 -040080.. cmdoption:: -j N
81
82 Use *N* workers to compile the files within the given directory.
83 If ``0`` is used, then the result of :func:`os.cpu_count()`
84 will be used.
Benjamin Peterson344ff4a2014-08-19 16:13:26 -050085
Benjamin Peterson42aa93b2017-12-09 10:26:52 -080086.. cmdoption:: --invalidation-mode [timestamp|checked-hash|unchecked-hash]
87
88 Control how the generated pycs will be invalidated at runtime. The default
89 setting, ``timestamp``, means that ``.pyc`` files with the source timestamp
90 and size embedded will be generated. The ``checked-hash`` and
91 ``unchecked-hash`` values cause hash-based pycs to be generated. Hash-based
92 pycs embed a hash of the source file contents rather than a timestamp. See
93 :ref:`pyc-invalidation` for more information on how Python validates bytecode
94 cache files at runtime.
95
Éric Araujo930df312010-12-16 06:28:48 +000096.. versionchanged:: 3.2
97 Added the ``-i``, ``-b`` and ``-h`` options.
Éric Araujof68fa052010-12-16 02:10:11 +000098
Benjamin Peterson344ff4a2014-08-19 16:13:26 -050099.. versionchanged:: 3.5
Yury Selivanovf03d50c2015-09-09 09:32:07 -0400100 Added the ``-j``, ``-r``, and ``-qq`` options. ``-q`` option
Serhiy Storchaka2446eab2015-09-13 21:09:17 +0300101 was changed to a multilevel value. ``-b`` will always produce a
Yury Selivanovf03d50c2015-09-09 09:32:07 -0400102 byte-code file ending in ``.pyc``, never ``.pyo``.
Brett Cannonf299abd2015-04-13 14:21:02 -0400103
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800104.. versionchanged:: 3.7
105 Added the ``--invalidation-mode`` parameter.
106
Benjamin Peterson344ff4a2014-08-19 16:13:26 -0500107
Éric Araujo01606de2011-03-26 03:22:55 +0100108There is no command-line option to control the optimization level used by the
109:func:`compile` function, because the Python interpreter itself already
110provides the option: :program:`python -O -m compileall`.
Éric Araujo713d3032010-11-18 16:38:46 +0000111
112Public functions
113----------------
Georg Brandl116aa622007-08-15 14:28:22 +0000114
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800115.. function:: compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1, workers=1, invalidation_mode=py_compile.PycInvalidationMode.TIMESTAMP)
Georg Brandl116aa622007-08-15 14:28:22 +0000116
117 Recursively descend the directory tree named by *dir*, compiling all :file:`.py`
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800118 files along the way. Return a true value if all the files compiled successfully,
119 and a false value otherwise.
R. David Murray94f58c32010-12-17 16:29:07 +0000120
121 The *maxlevels* parameter is used to limit the depth of the recursion; it
122 defaults to ``10``.
123
124 If *ddir* is given, it is prepended to the path to each file being compiled
125 for use in compilation time tracebacks, and is also compiled in to the
126 byte-code file, where it will be used in tracebacks and other messages in
127 cases where the source file does not exist at the time the byte-code file is
128 executed.
129
Georg Brandl116aa622007-08-15 14:28:22 +0000130 If *force* is true, modules are re-compiled even if the timestamps are up to
131 date.
132
R. David Murray94f58c32010-12-17 16:29:07 +0000133 If *rx* is given, its search method is called on the complete path to each
134 file considered for compilation, and if it returns a true value, the file
135 is skipped.
Georg Brandl116aa622007-08-15 14:28:22 +0000136
Berker Peksag6554b862014-10-15 11:10:57 +0300137 If *quiet* is ``False`` or ``0`` (the default), the filenames and other
138 information are printed to standard out. Set to ``1``, only errors are
139 printed. Set to ``2``, all output is suppressed.
Georg Brandl116aa622007-08-15 14:28:22 +0000140
R. David Murray94f58c32010-12-17 16:29:07 +0000141 If *legacy* is true, byte-code files are written to their legacy locations
142 and names, which may overwrite byte-code files created by another version of
143 Python. The default is to write files to their :pep:`3147` locations and
144 names, which allows byte-code files from multiple versions of Python to
145 coexist.
Georg Brandl116aa622007-08-15 14:28:22 +0000146
Georg Brandl8334fd92010-12-04 10:26:46 +0000147 *optimize* specifies the optimization level for the compiler. It is passed to
148 the built-in :func:`compile` function.
Barry Warsaw28a691b2010-04-17 00:19:56 +0000149
Brett Cannonf1a8df02014-09-12 10:39:48 -0400150 The argument *workers* specifies how many workers are used to
151 compile files in parallel. The default is to not use multiple workers.
152 If the platform can't use multiple workers and *workers* argument is given,
Berker Peksagd86ef052015-04-22 09:39:19 +0300153 then sequential compilation will be used as a fallback. If *workers* is
154 lower than ``0``, a :exc:`ValueError` will be raised.
Brett Cannonf1a8df02014-09-12 10:39:48 -0400155
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800156 *invalidation_mode* should be a member of the
157 :class:`py_compile.PycInvalidationMode` enum and controls how the generated
158 pycs are invalidated at runtime.
159
Georg Brandl8334fd92010-12-04 10:26:46 +0000160 .. versionchanged:: 3.2
Éric Araujo930df312010-12-16 06:28:48 +0000161 Added the *legacy* and *optimize* parameter.
162
Brett Cannonf1a8df02014-09-12 10:39:48 -0400163 .. versionchanged:: 3.5
164 Added the *workers* parameter.
165
Berker Peksag6554b862014-10-15 11:10:57 +0300166 .. versionchanged:: 3.5
167 *quiet* parameter was changed to a multilevel value.
Éric Araujo930df312010-12-16 06:28:48 +0000168
Brett Cannonf299abd2015-04-13 14:21:02 -0400169 .. versionchanged:: 3.5
170 The *legacy* parameter only writes out ``.pyc`` files, not ``.pyo`` files
171 no matter what the value of *optimize* is.
172
Berker Peksag812a2b62016-10-01 00:54:18 +0300173 .. versionchanged:: 3.6
174 Accepts a :term:`path-like object`.
175
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800176 .. versionchanged:: 3.7
177 The *invalidation_mode* parameter was added.
178
179.. function:: compile_file(fullname, ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1, invalidation_mode=py_compile.PycInvalidationMode.TIMESTAMP)
Éric Araujo930df312010-12-16 06:28:48 +0000180
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800181 Compile the file with path *fullname*. Return a true value if the file
182 compiled successfully, and a false value otherwise.
Éric Araujo930df312010-12-16 06:28:48 +0000183
R. David Murray94f58c32010-12-17 16:29:07 +0000184 If *ddir* is given, it is prepended to the path to the file being compiled
185 for use in compilation time tracebacks, and is also compiled in to the
186 byte-code file, where it will be used in tracebacks and other messages in
187 cases where the source file does not exist at the time the byte-code file is
188 executed.
Éric Araujo930df312010-12-16 06:28:48 +0000189
R. David Murray8b24aac2011-02-11 22:37:16 +0000190 If *rx* is given, its search method is passed the full path name to the
R. David Murray94f58c32010-12-17 16:29:07 +0000191 file being compiled, and if it returns a true value, the file is not
192 compiled and ``True`` is returned.
Éric Araujo930df312010-12-16 06:28:48 +0000193
Berker Peksag6554b862014-10-15 11:10:57 +0300194 If *quiet* is ``False`` or ``0`` (the default), the filenames and other
195 information are printed to standard out. Set to ``1``, only errors are
196 printed. Set to ``2``, all output is suppressed.
R. David Murray94f58c32010-12-17 16:29:07 +0000197
198 If *legacy* is true, byte-code files are written to their legacy locations
199 and names, which may overwrite byte-code files created by another version of
200 Python. The default is to write files to their :pep:`3147` locations and
201 names, which allows byte-code files from multiple versions of Python to
202 coexist.
Éric Araujo930df312010-12-16 06:28:48 +0000203
204 *optimize* specifies the optimization level for the compiler. It is passed to
205 the built-in :func:`compile` function.
206
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800207 *invalidation_mode* should be a member of the
208 :class:`py_compile.PycInvalidationMode` enum and controls how the generated
209 pycs are invalidated at runtime.
210
Éric Araujo930df312010-12-16 06:28:48 +0000211 .. versionadded:: 3.2
Georg Brandl8334fd92010-12-04 10:26:46 +0000212
Berker Peksag6554b862014-10-15 11:10:57 +0300213 .. versionchanged:: 3.5
214 *quiet* parameter was changed to a multilevel value.
Georg Brandl8334fd92010-12-04 10:26:46 +0000215
Brett Cannonf299abd2015-04-13 14:21:02 -0400216 .. versionchanged:: 3.5
217 The *legacy* parameter only writes out ``.pyc`` files, not ``.pyo`` files
218 no matter what the value of *optimize* is.
219
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800220 .. versionchanged:: 3.7
221 The *invalidation_mode* parameter was added.
222
223.. function:: compile_path(skip_curdir=True, maxlevels=0, force=False, quiet=0, legacy=False, optimize=-1, invalidation_mode=py_compile.PycInvalidationMode.TIMESTAMP)
Georg Brandl116aa622007-08-15 14:28:22 +0000224
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800225 Byte-compile all the :file:`.py` files found along ``sys.path``. Return a
226 true value if all the files compiled successfully, and a false value otherwise.
227
228 If *skip_curdir* is true (the default), the current directory is not included
R. David Murray94f58c32010-12-17 16:29:07 +0000229 in the search. All other parameters are passed to the :func:`compile_dir`
230 function. Note that unlike the other compile functions, ``maxlevels``
231 defaults to ``0``.
Georg Brandl8334fd92010-12-04 10:26:46 +0000232
233 .. versionchanged:: 3.2
Éric Araujo930df312010-12-16 06:28:48 +0000234 Added the *legacy* and *optimize* parameter.
Georg Brandl8334fd92010-12-04 10:26:46 +0000235
Berker Peksag6554b862014-10-15 11:10:57 +0300236 .. versionchanged:: 3.5
237 *quiet* parameter was changed to a multilevel value.
Georg Brandl116aa622007-08-15 14:28:22 +0000238
Brett Cannonf299abd2015-04-13 14:21:02 -0400239 .. versionchanged:: 3.5
240 The *legacy* parameter only writes out ``.pyc`` files, not ``.pyo`` files
241 no matter what the value of *optimize* is.
242
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800243 .. versionchanged:: 3.7
244 The *invalidation_mode* parameter was added.
245
Georg Brandl116aa622007-08-15 14:28:22 +0000246To force a recompile of all the :file:`.py` files in the :file:`Lib/`
247subdirectory and all its subdirectories::
248
249 import compileall
250
251 compileall.compile_dir('Lib/', force=True)
252
253 # Perform same compilation, excluding files in .svn directories.
254 import re
Georg Brandl1aca9532013-04-14 12:02:43 +0200255 compileall.compile_dir('Lib/', rx=re.compile(r'[/\\][.]svn'), force=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000256
Berker Peksag812a2b62016-10-01 00:54:18 +0300257 # pathlib.Path objects can also be used.
258 import pathlib
259 compileall.compile_dir(pathlib.Path('Lib/'), force=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000260
261.. seealso::
262
263 Module :mod:`py_compile`
264 Byte-compile a single source file.