blob: 679c2b48580f763c4728c0a41818955eba076f9c [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 +000011
12This module provides some utility functions to support installing Python
R. David Murray94f58c32010-12-17 16:29:07 +000013libraries. These functions compile Python source files in a directory tree.
14This module can be used to create the cached byte-code files at library
15installation time, which makes them available for use even by users who don't
16have write permission to the library directories.
Georg Brandl116aa622007-08-15 14:28:22 +000017
Georg Brandl116aa622007-08-15 14:28:22 +000018
Éric Araujo713d3032010-11-18 16:38:46 +000019Command-line use
20----------------
21
22This module can work as a script (using :program:`python -m compileall`) to
23compile Python sources.
24
25.. program:: compileall
26
Georg Brandlf5c801f2014-03-18 07:44:07 +010027.. cmdoption:: directory ...
28 file ...
Éric Araujo713d3032010-11-18 16:38:46 +000029
30 Positional arguments are files to compile or directories that contain
31 source files, traversed recursively. If no argument is given, behave as if
32 the command line was ``-l <directories from sys.path>``.
33
34.. cmdoption:: -l
35
R. David Murray94f58c32010-12-17 16:29:07 +000036 Do not recurse into subdirectories, only compile source code files directly
37 contained in the named or implied directories.
Éric Araujo713d3032010-11-18 16:38:46 +000038
39.. cmdoption:: -f
40
41 Force rebuild even if timestamps are up-to-date.
42
43.. cmdoption:: -q
44
Berker Peksag6554b862014-10-15 11:10:57 +030045 Do not print the list of files compiled. If passed once, error messages will
46 still be printed. If passed twice (``-qq``), all output is suppressed.
Éric Araujo713d3032010-11-18 16:38:46 +000047
48.. cmdoption:: -d destdir
49
R. David Murray94f58c32010-12-17 16:29:07 +000050 Directory prepended to the path to each file being compiled. This will
51 appear in compilation time tracebacks, and is also compiled in to the
52 byte-code file, where it will be used in tracebacks and other messages in
53 cases where the source file does not exist at the time the byte-code file is
54 executed.
Éric Araujo713d3032010-11-18 16:38:46 +000055
56.. cmdoption:: -x regex
57
R. David Murray94f58c32010-12-17 16:29:07 +000058 regex is used to search the full path to each file considered for
59 compilation, and if the regex produces a match, the file is skipped.
Éric Araujo713d3032010-11-18 16:38:46 +000060
61.. cmdoption:: -i list
62
R. David Murray94f58c32010-12-17 16:29:07 +000063 Read the file ``list`` and add each line that it contains to the list of
64 files and directories to compile. If ``list`` is ``-``, read lines from
65 ``stdin``.
Éric Araujo713d3032010-11-18 16:38:46 +000066
67.. cmdoption:: -b
68
R. David Murray94f58c32010-12-17 16:29:07 +000069 Write the byte-code files to their legacy locations and names, which may
70 overwrite byte-code files created by another version of Python. The default
71 is to write files to their :pep:`3147` locations and names, which allows
72 byte-code files from multiple versions of Python to coexist.
Éric Araujo713d3032010-11-18 16:38:46 +000073
Benjamin Peterson344ff4a2014-08-19 16:13:26 -050074.. cmdoption:: -r
75
76 Control the maximum recursion level for subdirectories.
77 If this is given, then ``-l`` option will not be taken into account.
78 :program:`python -m compileall <directory> -r 0` is equivalent to
79 :program:`python -m compileall <directory> -l`.
80
Brett Cannonf1a8df02014-09-12 10:39:48 -040081.. cmdoption:: -j N
82
83 Use *N* workers to compile the files within the given directory.
84 If ``0`` is used, then the result of :func:`os.cpu_count()`
85 will be used.
Benjamin Peterson344ff4a2014-08-19 16:13:26 -050086
Éric Araujo930df312010-12-16 06:28:48 +000087.. versionchanged:: 3.2
88 Added the ``-i``, ``-b`` and ``-h`` options.
Éric Araujof68fa052010-12-16 02:10:11 +000089
Benjamin Peterson344ff4a2014-08-19 16:13:26 -050090.. versionchanged:: 3.5
Yury Selivanovf03d50c2015-09-09 09:32:07 -040091 Added the ``-j``, ``-r``, and ``-qq`` options. ``-q`` option
Serhiy Storchaka2446eab2015-09-13 21:09:17 +030092 was changed to a multilevel value. ``-b`` will always produce a
Yury Selivanovf03d50c2015-09-09 09:32:07 -040093 byte-code file ending in ``.pyc``, never ``.pyo``.
Brett Cannonf299abd2015-04-13 14:21:02 -040094
Benjamin Peterson344ff4a2014-08-19 16:13:26 -050095
Éric Araujo01606de2011-03-26 03:22:55 +010096There is no command-line option to control the optimization level used by the
97:func:`compile` function, because the Python interpreter itself already
98provides the option: :program:`python -O -m compileall`.
Éric Araujo713d3032010-11-18 16:38:46 +000099
100Public functions
101----------------
Georg Brandl116aa622007-08-15 14:28:22 +0000102
Berker Peksag6554b862014-10-15 11:10:57 +0300103.. function:: compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1, workers=1)
Georg Brandl116aa622007-08-15 14:28:22 +0000104
105 Recursively descend the directory tree named by *dir*, compiling all :file:`.py`
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800106 files along the way. Return a true value if all the files compiled successfully,
107 and a false value otherwise.
R. David Murray94f58c32010-12-17 16:29:07 +0000108
109 The *maxlevels* parameter is used to limit the depth of the recursion; it
110 defaults to ``10``.
111
112 If *ddir* is given, it is prepended to the path to each file being compiled
113 for use in compilation time tracebacks, and is also compiled in to the
114 byte-code file, where it will be used in tracebacks and other messages in
115 cases where the source file does not exist at the time the byte-code file is
116 executed.
117
Georg Brandl116aa622007-08-15 14:28:22 +0000118 If *force* is true, modules are re-compiled even if the timestamps are up to
119 date.
120
R. David Murray94f58c32010-12-17 16:29:07 +0000121 If *rx* is given, its search method is called on the complete path to each
122 file considered for compilation, and if it returns a true value, the file
123 is skipped.
Georg Brandl116aa622007-08-15 14:28:22 +0000124
Berker Peksag6554b862014-10-15 11:10:57 +0300125 If *quiet* is ``False`` or ``0`` (the default), the filenames and other
126 information are printed to standard out. Set to ``1``, only errors are
127 printed. Set to ``2``, all output is suppressed.
Georg Brandl116aa622007-08-15 14:28:22 +0000128
R. David Murray94f58c32010-12-17 16:29:07 +0000129 If *legacy* is true, byte-code files are written to their legacy locations
130 and names, which may overwrite byte-code files created by another version of
131 Python. The default is to write files to their :pep:`3147` locations and
132 names, which allows byte-code files from multiple versions of Python to
133 coexist.
Georg Brandl116aa622007-08-15 14:28:22 +0000134
Georg Brandl8334fd92010-12-04 10:26:46 +0000135 *optimize* specifies the optimization level for the compiler. It is passed to
136 the built-in :func:`compile` function.
Barry Warsaw28a691b2010-04-17 00:19:56 +0000137
Brett Cannonf1a8df02014-09-12 10:39:48 -0400138 The argument *workers* specifies how many workers are used to
139 compile files in parallel. The default is to not use multiple workers.
140 If the platform can't use multiple workers and *workers* argument is given,
Berker Peksagd86ef052015-04-22 09:39:19 +0300141 then sequential compilation will be used as a fallback. If *workers* is
142 lower than ``0``, a :exc:`ValueError` will be raised.
Brett Cannonf1a8df02014-09-12 10:39:48 -0400143
Georg Brandl8334fd92010-12-04 10:26:46 +0000144 .. versionchanged:: 3.2
Éric Araujo930df312010-12-16 06:28:48 +0000145 Added the *legacy* and *optimize* parameter.
146
Brett Cannonf1a8df02014-09-12 10:39:48 -0400147 .. versionchanged:: 3.5
148 Added the *workers* parameter.
149
Berker Peksag6554b862014-10-15 11:10:57 +0300150 .. versionchanged:: 3.5
151 *quiet* parameter was changed to a multilevel value.
Éric Araujo930df312010-12-16 06:28:48 +0000152
Brett Cannonf299abd2015-04-13 14:21:02 -0400153 .. versionchanged:: 3.5
154 The *legacy* parameter only writes out ``.pyc`` files, not ``.pyo`` files
155 no matter what the value of *optimize* is.
156
Berker Peksag6554b862014-10-15 11:10:57 +0300157.. function:: compile_file(fullname, ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1)
Éric Araujo930df312010-12-16 06:28:48 +0000158
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800159 Compile the file with path *fullname*. Return a true value if the file
160 compiled successfully, and a false value otherwise.
Éric Araujo930df312010-12-16 06:28:48 +0000161
R. David Murray94f58c32010-12-17 16:29:07 +0000162 If *ddir* is given, it is prepended to the path to the file being compiled
163 for use in compilation time tracebacks, and is also compiled in to the
164 byte-code file, where it will be used in tracebacks and other messages in
165 cases where the source file does not exist at the time the byte-code file is
166 executed.
Éric Araujo930df312010-12-16 06:28:48 +0000167
R. David Murray8b24aac2011-02-11 22:37:16 +0000168 If *rx* is given, its search method is passed the full path name to the
R. David Murray94f58c32010-12-17 16:29:07 +0000169 file being compiled, and if it returns a true value, the file is not
170 compiled and ``True`` is returned.
Éric Araujo930df312010-12-16 06:28:48 +0000171
Berker Peksag6554b862014-10-15 11:10:57 +0300172 If *quiet* is ``False`` or ``0`` (the default), the filenames and other
173 information are printed to standard out. Set to ``1``, only errors are
174 printed. Set to ``2``, all output is suppressed.
R. David Murray94f58c32010-12-17 16:29:07 +0000175
176 If *legacy* is true, byte-code files are written to their legacy locations
177 and names, which may overwrite byte-code files created by another version of
178 Python. The default is to write files to their :pep:`3147` locations and
179 names, which allows byte-code files from multiple versions of Python to
180 coexist.
Éric Araujo930df312010-12-16 06:28:48 +0000181
182 *optimize* specifies the optimization level for the compiler. It is passed to
183 the built-in :func:`compile` function.
184
185 .. versionadded:: 3.2
Georg Brandl8334fd92010-12-04 10:26:46 +0000186
Berker Peksag6554b862014-10-15 11:10:57 +0300187 .. versionchanged:: 3.5
188 *quiet* parameter was changed to a multilevel value.
Georg Brandl8334fd92010-12-04 10:26:46 +0000189
Brett Cannonf299abd2015-04-13 14:21:02 -0400190 .. versionchanged:: 3.5
191 The *legacy* parameter only writes out ``.pyc`` files, not ``.pyo`` files
192 no matter what the value of *optimize* is.
193
Berker Peksag6554b862014-10-15 11:10:57 +0300194.. function:: compile_path(skip_curdir=True, maxlevels=0, force=False, quiet=0, legacy=False, optimize=-1)
Georg Brandl116aa622007-08-15 14:28:22 +0000195
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800196 Byte-compile all the :file:`.py` files found along ``sys.path``. Return a
197 true value if all the files compiled successfully, and a false value otherwise.
198
199 If *skip_curdir* is true (the default), the current directory is not included
R. David Murray94f58c32010-12-17 16:29:07 +0000200 in the search. All other parameters are passed to the :func:`compile_dir`
201 function. Note that unlike the other compile functions, ``maxlevels``
202 defaults to ``0``.
Georg Brandl8334fd92010-12-04 10:26:46 +0000203
204 .. versionchanged:: 3.2
Éric Araujo930df312010-12-16 06:28:48 +0000205 Added the *legacy* and *optimize* parameter.
Georg Brandl8334fd92010-12-04 10:26:46 +0000206
Berker Peksag6554b862014-10-15 11:10:57 +0300207 .. versionchanged:: 3.5
208 *quiet* parameter was changed to a multilevel value.
Georg Brandl116aa622007-08-15 14:28:22 +0000209
Brett Cannonf299abd2015-04-13 14:21:02 -0400210 .. versionchanged:: 3.5
211 The *legacy* parameter only writes out ``.pyc`` files, not ``.pyo`` files
212 no matter what the value of *optimize* is.
213
Georg Brandl116aa622007-08-15 14:28:22 +0000214To force a recompile of all the :file:`.py` files in the :file:`Lib/`
215subdirectory and all its subdirectories::
216
217 import compileall
218
219 compileall.compile_dir('Lib/', force=True)
220
221 # Perform same compilation, excluding files in .svn directories.
222 import re
Georg Brandl1aca9532013-04-14 12:02:43 +0200223 compileall.compile_dir('Lib/', rx=re.compile(r'[/\\][.]svn'), force=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000224
225
226.. seealso::
227
228 Module :mod:`py_compile`
229 Byte-compile a single source file.