blob: de34664acb84ab7d815c8525f918fe748ac6f21b [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
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +020055.. cmdoption:: -s strip_prefix
56.. cmdoption:: -p prepend_prefix
57
58 Remove (``-s``) or append (``-p``) the given prefix of paths
59 recorded in the ``.pyc`` files.
60 Cannot be combined with ``-d``.
61
Éric Araujo713d3032010-11-18 16:38:46 +000062.. cmdoption:: -x regex
63
R. David Murray94f58c32010-12-17 16:29:07 +000064 regex is used to search the full path to each file considered for
65 compilation, and if the regex produces a match, the file is skipped.
Éric Araujo713d3032010-11-18 16:38:46 +000066
67.. cmdoption:: -i list
68
R. David Murray94f58c32010-12-17 16:29:07 +000069 Read the file ``list`` and add each line that it contains to the list of
70 files and directories to compile. If ``list`` is ``-``, read lines from
71 ``stdin``.
Éric Araujo713d3032010-11-18 16:38:46 +000072
73.. cmdoption:: -b
74
R. David Murray94f58c32010-12-17 16:29:07 +000075 Write the byte-code files to their legacy locations and names, which may
76 overwrite byte-code files created by another version of Python. The default
77 is to write files to their :pep:`3147` locations and names, which allows
78 byte-code files from multiple versions of Python to coexist.
Éric Araujo713d3032010-11-18 16:38:46 +000079
Benjamin Peterson344ff4a2014-08-19 16:13:26 -050080.. cmdoption:: -r
81
82 Control the maximum recursion level for subdirectories.
83 If this is given, then ``-l`` option will not be taken into account.
84 :program:`python -m compileall <directory> -r 0` is equivalent to
85 :program:`python -m compileall <directory> -l`.
86
Brett Cannonf1a8df02014-09-12 10:39:48 -040087.. cmdoption:: -j N
88
89 Use *N* workers to compile the files within the given directory.
90 If ``0`` is used, then the result of :func:`os.cpu_count()`
91 will be used.
Benjamin Peterson344ff4a2014-08-19 16:13:26 -050092
Benjamin Peterson42aa93b2017-12-09 10:26:52 -080093.. cmdoption:: --invalidation-mode [timestamp|checked-hash|unchecked-hash]
94
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -040095 Control how the generated byte-code files are invalidated at runtime.
96 The ``timestamp`` value, means that ``.pyc`` files with the source timestamp
Benjamin Peterson42aa93b2017-12-09 10:26:52 -080097 and size embedded will be generated. The ``checked-hash`` and
98 ``unchecked-hash`` values cause hash-based pycs to be generated. Hash-based
99 pycs embed a hash of the source file contents rather than a timestamp. See
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -0400100 :ref:`pyc-invalidation` for more information on how Python validates
101 bytecode cache files at runtime.
102 The default is ``timestamp`` if the :envvar:`SOURCE_DATE_EPOCH` environment
103 variable is not set, and ``checked-hash`` if the ``SOURCE_DATE_EPOCH``
104 environment variable is set.
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800105
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200106.. cmdoption:: -o level
107
108 Compile with the given optimization level. May be used multiple times
109 to compile for multiple levels at a time (for example,
110 ``compileall -o 1 -o 2``).
111
112.. cmdoption:: -e dir
113
114 Ignore symlinks pointing outside the given directory.
115
Lumír 'Frenzy' Balhare77d4282020-05-14 16:17:22 +0200116.. cmdoption:: --hardlink-dupes
117
118 If two ``.pyc`` files with different optimization level have
119 the same content, use hard links to consolidate duplicate files.
120
Éric Araujo930df312010-12-16 06:28:48 +0000121.. versionchanged:: 3.2
122 Added the ``-i``, ``-b`` and ``-h`` options.
Éric Araujof68fa052010-12-16 02:10:11 +0000123
Benjamin Peterson344ff4a2014-08-19 16:13:26 -0500124.. versionchanged:: 3.5
Yury Selivanovf03d50c2015-09-09 09:32:07 -0400125 Added the ``-j``, ``-r``, and ``-qq`` options. ``-q`` option
Serhiy Storchaka2446eab2015-09-13 21:09:17 +0300126 was changed to a multilevel value. ``-b`` will always produce a
Yury Selivanovf03d50c2015-09-09 09:32:07 -0400127 byte-code file ending in ``.pyc``, never ``.pyo``.
Brett Cannonf299abd2015-04-13 14:21:02 -0400128
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800129.. versionchanged:: 3.7
NAKAMURA Osamu0983fcd2019-03-05 13:43:43 +0900130 Added the ``--invalidation-mode`` option.
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800131
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200132.. versionchanged:: 3.9
Lumír 'Frenzy' Balhare77d4282020-05-14 16:17:22 +0200133 Added the ``-s``, ``-p``, ``-e`` and ``--hardlink-dupes`` options.
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200134 Raised the default recursion limit from 10 to
135 :py:func:`sys.getrecursionlimit()`.
136 Added the possibility to specify the ``-o`` option multiple times.
137
Benjamin Peterson344ff4a2014-08-19 16:13:26 -0500138
Éric Araujo01606de2011-03-26 03:22:55 +0100139There is no command-line option to control the optimization level used by the
140:func:`compile` function, because the Python interpreter itself already
141provides the option: :program:`python -O -m compileall`.
Éric Araujo713d3032010-11-18 16:38:46 +0000142
Carl Meyerb193fa92018-06-15 22:40:56 -0600143Similarly, the :func:`compile` function respects the :attr:`sys.pycache_prefix`
144setting. The generated bytecode cache will only be useful if :func:`compile` is
145run with the same :attr:`sys.pycache_prefix` (if any) that will be used at
146runtime.
147
Éric Araujo713d3032010-11-18 16:38:46 +0000148Public functions
149----------------
Georg Brandl116aa622007-08-15 14:28:22 +0000150
Andre Delfinodcc997c2020-12-16 22:37:28 -0300151.. function:: compile_dir(dir, maxlevels=sys.getrecursionlimit(), ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1, workers=1, invalidation_mode=None, *, stripdir=None, prependdir=None, limit_sl_dest=None, hardlink_dupes=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000152
153 Recursively descend the directory tree named by *dir*, compiling all :file:`.py`
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800154 files along the way. Return a true value if all the files compiled successfully,
155 and a false value otherwise.
R. David Murray94f58c32010-12-17 16:29:07 +0000156
157 The *maxlevels* parameter is used to limit the depth of the recursion; it
Shantanua2b3cdd2020-05-15 14:28:23 -0700158 defaults to ``sys.getrecursionlimit()``.
R. David Murray94f58c32010-12-17 16:29:07 +0000159
160 If *ddir* is given, it is prepended to the path to each file being compiled
161 for use in compilation time tracebacks, and is also compiled in to the
162 byte-code file, where it will be used in tracebacks and other messages in
163 cases where the source file does not exist at the time the byte-code file is
164 executed.
165
Georg Brandl116aa622007-08-15 14:28:22 +0000166 If *force* is true, modules are re-compiled even if the timestamps are up to
167 date.
168
Miss Islington (bot)5dffd792021-05-04 12:57:35 -0700169 If *rx* is given, its ``search`` method is called on the complete path to each
R. David Murray94f58c32010-12-17 16:29:07 +0000170 file considered for compilation, and if it returns a true value, the file
Miss Islington (bot)5dffd792021-05-04 12:57:35 -0700171 is skipped. This can be used to exclude files matching a regular expression,
172 given as a :ref:`re.Pattern <re-objects>` object.
Georg Brandl116aa622007-08-15 14:28:22 +0000173
Berker Peksag6554b862014-10-15 11:10:57 +0300174 If *quiet* is ``False`` or ``0`` (the default), the filenames and other
175 information are printed to standard out. Set to ``1``, only errors are
176 printed. Set to ``2``, all output is suppressed.
Georg Brandl116aa622007-08-15 14:28:22 +0000177
R. David Murray94f58c32010-12-17 16:29:07 +0000178 If *legacy* is true, byte-code files are written to their legacy locations
179 and names, which may overwrite byte-code files created by another version of
180 Python. The default is to write files to their :pep:`3147` locations and
181 names, which allows byte-code files from multiple versions of Python to
182 coexist.
Georg Brandl116aa622007-08-15 14:28:22 +0000183
Georg Brandl8334fd92010-12-04 10:26:46 +0000184 *optimize* specifies the optimization level for the compiler. It is passed to
Lumír 'Frenzy' Balharadc72bb2020-05-18 15:23:37 +0200185 the built-in :func:`compile` function. Accepts also a sequence of optimization
186 levels which lead to multiple compilations of one :file:`.py` file in one call.
Barry Warsaw28a691b2010-04-17 00:19:56 +0000187
Brett Cannonf1a8df02014-09-12 10:39:48 -0400188 The argument *workers* specifies how many workers are used to
189 compile files in parallel. The default is to not use multiple workers.
190 If the platform can't use multiple workers and *workers* argument is given,
Antoine Pitrou1a2dd822019-05-15 23:45:18 +0200191 then sequential compilation will be used as a fallback. If *workers*
192 is 0, the number of cores in the system is used. If *workers* is
Berker Peksagd86ef052015-04-22 09:39:19 +0300193 lower than ``0``, a :exc:`ValueError` will be raised.
Brett Cannonf1a8df02014-09-12 10:39:48 -0400194
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800195 *invalidation_mode* should be a member of the
196 :class:`py_compile.PycInvalidationMode` enum and controls how the generated
197 pycs are invalidated at runtime.
198
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200199 The *stripdir*, *prependdir* and *limit_sl_dest* arguments correspond to
200 the ``-s``, ``-p`` and ``-e`` options described above.
201 They may be specified as ``str``, ``bytes`` or :py:class:`os.PathLike`.
202
Lumír 'Frenzy' Balhare77d4282020-05-14 16:17:22 +0200203 If *hardlink_dupes* is true and two ``.pyc`` files with different optimization
204 level have the same content, use hard links to consolidate duplicate files.
205
Georg Brandl8334fd92010-12-04 10:26:46 +0000206 .. versionchanged:: 3.2
Éric Araujo930df312010-12-16 06:28:48 +0000207 Added the *legacy* and *optimize* parameter.
208
Brett Cannonf1a8df02014-09-12 10:39:48 -0400209 .. versionchanged:: 3.5
210 Added the *workers* parameter.
211
Berker Peksag6554b862014-10-15 11:10:57 +0300212 .. versionchanged:: 3.5
213 *quiet* parameter was changed to a multilevel value.
Éric Araujo930df312010-12-16 06:28:48 +0000214
Brett Cannonf299abd2015-04-13 14:21:02 -0400215 .. versionchanged:: 3.5
216 The *legacy* parameter only writes out ``.pyc`` files, not ``.pyo`` files
217 no matter what the value of *optimize* is.
218
Berker Peksag812a2b62016-10-01 00:54:18 +0300219 .. versionchanged:: 3.6
220 Accepts a :term:`path-like object`.
221
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800222 .. versionchanged:: 3.7
223 The *invalidation_mode* parameter was added.
224
Hai Shi68e495d2019-08-14 17:03:11 -0500225 .. versionchanged:: 3.7.2
226 The *invalidation_mode* parameter's default value is updated to None.
227
Antoine Pitrou1a2dd822019-05-15 23:45:18 +0200228 .. versionchanged:: 3.8
229 Setting *workers* to 0 now chooses the optimal number of cores.
230
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200231 .. versionchanged:: 3.9
Lumír 'Frenzy' Balhare77d4282020-05-14 16:17:22 +0200232 Added *stripdir*, *prependdir*, *limit_sl_dest* and *hardlink_dupes* arguments.
Shantanua2b3cdd2020-05-15 14:28:23 -0700233 Default value of *maxlevels* was changed from ``10`` to ``sys.getrecursionlimit()``
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200234
Andre Delfinodcc997c2020-12-16 22:37:28 -0300235.. function:: compile_file(fullname, ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1, invalidation_mode=None, *, stripdir=None, prependdir=None, limit_sl_dest=None, hardlink_dupes=False)
Éric Araujo930df312010-12-16 06:28:48 +0000236
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800237 Compile the file with path *fullname*. Return a true value if the file
238 compiled successfully, and a false value otherwise.
Éric Araujo930df312010-12-16 06:28:48 +0000239
R. David Murray94f58c32010-12-17 16:29:07 +0000240 If *ddir* is given, it is prepended to the path to the file being compiled
241 for use in compilation time tracebacks, and is also compiled in to the
242 byte-code file, where it will be used in tracebacks and other messages in
243 cases where the source file does not exist at the time the byte-code file is
244 executed.
Éric Araujo930df312010-12-16 06:28:48 +0000245
Miss Islington (bot)5dffd792021-05-04 12:57:35 -0700246 If *rx* is given, its ``search`` method is passed the full path name to the
R. David Murray94f58c32010-12-17 16:29:07 +0000247 file being compiled, and if it returns a true value, the file is not
Miss Islington (bot)5dffd792021-05-04 12:57:35 -0700248 compiled and ``True`` is returned. This can be used to exclude files matching
249 a regular expression, given as a :ref:`re.Pattern <re-objects>` object.
Éric Araujo930df312010-12-16 06:28:48 +0000250
Berker Peksag6554b862014-10-15 11:10:57 +0300251 If *quiet* is ``False`` or ``0`` (the default), the filenames and other
252 information are printed to standard out. Set to ``1``, only errors are
253 printed. Set to ``2``, all output is suppressed.
R. David Murray94f58c32010-12-17 16:29:07 +0000254
255 If *legacy* is true, byte-code files are written to their legacy locations
256 and names, which may overwrite byte-code files created by another version of
257 Python. The default is to write files to their :pep:`3147` locations and
258 names, which allows byte-code files from multiple versions of Python to
259 coexist.
Éric Araujo930df312010-12-16 06:28:48 +0000260
261 *optimize* specifies the optimization level for the compiler. It is passed to
Lumír 'Frenzy' Balharadc72bb2020-05-18 15:23:37 +0200262 the built-in :func:`compile` function. Accepts also a sequence of optimization
263 levels which lead to multiple compilations of one :file:`.py` file in one call.
Éric Araujo930df312010-12-16 06:28:48 +0000264
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800265 *invalidation_mode* should be a member of the
266 :class:`py_compile.PycInvalidationMode` enum and controls how the generated
267 pycs are invalidated at runtime.
268
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200269 The *stripdir*, *prependdir* and *limit_sl_dest* arguments correspond to
270 the ``-s``, ``-p`` and ``-e`` options described above.
271 They may be specified as ``str``, ``bytes`` or :py:class:`os.PathLike`.
272
Lumír 'Frenzy' Balhare77d4282020-05-14 16:17:22 +0200273 If *hardlink_dupes* is true and two ``.pyc`` files with different optimization
274 level have the same content, use hard links to consolidate duplicate files.
275
Éric Araujo930df312010-12-16 06:28:48 +0000276 .. versionadded:: 3.2
Georg Brandl8334fd92010-12-04 10:26:46 +0000277
Berker Peksag6554b862014-10-15 11:10:57 +0300278 .. versionchanged:: 3.5
279 *quiet* parameter was changed to a multilevel value.
Georg Brandl8334fd92010-12-04 10:26:46 +0000280
Brett Cannonf299abd2015-04-13 14:21:02 -0400281 .. versionchanged:: 3.5
282 The *legacy* parameter only writes out ``.pyc`` files, not ``.pyo`` files
283 no matter what the value of *optimize* is.
284
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800285 .. versionchanged:: 3.7
286 The *invalidation_mode* parameter was added.
287
Hai Shi68e495d2019-08-14 17:03:11 -0500288 .. versionchanged:: 3.7.2
289 The *invalidation_mode* parameter's default value is updated to None.
290
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200291 .. versionchanged:: 3.9
Lumír 'Frenzy' Balhare77d4282020-05-14 16:17:22 +0200292 Added *stripdir*, *prependdir*, *limit_sl_dest* and *hardlink_dupes* arguments.
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200293
Hai Shi68e495d2019-08-14 17:03:11 -0500294.. function:: compile_path(skip_curdir=True, maxlevels=0, force=False, quiet=0, legacy=False, optimize=-1, invalidation_mode=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000295
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800296 Byte-compile all the :file:`.py` files found along ``sys.path``. Return a
297 true value if all the files compiled successfully, and a false value otherwise.
298
299 If *skip_curdir* is true (the default), the current directory is not included
R. David Murray94f58c32010-12-17 16:29:07 +0000300 in the search. All other parameters are passed to the :func:`compile_dir`
301 function. Note that unlike the other compile functions, ``maxlevels``
302 defaults to ``0``.
Georg Brandl8334fd92010-12-04 10:26:46 +0000303
304 .. versionchanged:: 3.2
Éric Araujo930df312010-12-16 06:28:48 +0000305 Added the *legacy* and *optimize* parameter.
Georg Brandl8334fd92010-12-04 10:26:46 +0000306
Berker Peksag6554b862014-10-15 11:10:57 +0300307 .. versionchanged:: 3.5
308 *quiet* parameter was changed to a multilevel value.
Georg Brandl116aa622007-08-15 14:28:22 +0000309
Brett Cannonf299abd2015-04-13 14:21:02 -0400310 .. versionchanged:: 3.5
311 The *legacy* parameter only writes out ``.pyc`` files, not ``.pyo`` files
312 no matter what the value of *optimize* is.
313
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800314 .. versionchanged:: 3.7
315 The *invalidation_mode* parameter was added.
316
Hai Shi68e495d2019-08-14 17:03:11 -0500317 .. versionchanged:: 3.7.2
318 The *invalidation_mode* parameter's default value is updated to None.
319
Georg Brandl116aa622007-08-15 14:28:22 +0000320To force a recompile of all the :file:`.py` files in the :file:`Lib/`
321subdirectory and all its subdirectories::
322
323 import compileall
324
325 compileall.compile_dir('Lib/', force=True)
326
327 # Perform same compilation, excluding files in .svn directories.
328 import re
Georg Brandl1aca9532013-04-14 12:02:43 +0200329 compileall.compile_dir('Lib/', rx=re.compile(r'[/\\][.]svn'), force=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000330
Berker Peksag812a2b62016-10-01 00:54:18 +0300331 # pathlib.Path objects can also be used.
332 import pathlib
333 compileall.compile_dir(pathlib.Path('Lib/'), force=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000334
335.. seealso::
336
337 Module :mod:`py_compile`
338 Byte-compile a single source file.