Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :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 Brandl | c0a8f8c | 2014-10-02 08:38:39 +0200 | [diff] [blame] | 7 | **Source code:** :source:`Lib/compileall.py` |
| 8 | |
| 9 | -------------- |
| 10 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 11 | This module provides some utility functions to support installing Python |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 12 | libraries. These functions compile Python source files in a directory tree. |
| 13 | This module can be used to create the cached byte-code files at library |
| 14 | installation time, which makes them available for use even by users who don't |
| 15 | have write permission to the library directories. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 16 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 | |
Éric Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 18 | Command-line use |
| 19 | ---------------- |
| 20 | |
| 21 | This module can work as a script (using :program:`python -m compileall`) to |
| 22 | compile Python sources. |
| 23 | |
| 24 | .. program:: compileall |
| 25 | |
Georg Brandl | f5c801f | 2014-03-18 07:44:07 +0100 | [diff] [blame] | 26 | .. cmdoption:: directory ... |
| 27 | file ... |
Éric Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 28 | |
| 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 Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 35 | Do not recurse into subdirectories, only compile source code files directly |
| 36 | contained in the named or implied directories. |
Éric Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 37 | |
| 38 | .. cmdoption:: -f |
| 39 | |
| 40 | Force rebuild even if timestamps are up-to-date. |
| 41 | |
| 42 | .. cmdoption:: -q |
| 43 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 44 | 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 Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 46 | |
| 47 | .. cmdoption:: -d destdir |
| 48 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 49 | 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 Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 54 | |
| 55 | .. cmdoption:: -x regex |
| 56 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 57 | 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 Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 59 | |
| 60 | .. cmdoption:: -i list |
| 61 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 62 | 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 Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 65 | |
| 66 | .. cmdoption:: -b |
| 67 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 68 | 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 Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 72 | |
Benjamin Peterson | 344ff4a | 2014-08-19 16:13:26 -0500 | [diff] [blame] | 73 | .. 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 Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 80 | .. 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 Peterson | 344ff4a | 2014-08-19 16:13:26 -0500 | [diff] [blame] | 85 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 86 | .. cmdoption:: --invalidation-mode [timestamp|checked-hash|unchecked-hash] |
| 87 | |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame^] | 88 | Control how the generated byte-code files are invalidated at runtime. |
| 89 | The ``timestamp`` value, means that ``.pyc`` files with the source timestamp |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 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 |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame^] | 93 | :ref:`pyc-invalidation` for more information on how Python validates |
| 94 | bytecode cache files at runtime. |
| 95 | The default is ``timestamp`` if the :envvar:`SOURCE_DATE_EPOCH` environment |
| 96 | variable is not set, and ``checked-hash`` if the ``SOURCE_DATE_EPOCH`` |
| 97 | environment variable is set. |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 98 | |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 99 | .. versionchanged:: 3.2 |
| 100 | Added the ``-i``, ``-b`` and ``-h`` options. |
Éric Araujo | f68fa05 | 2010-12-16 02:10:11 +0000 | [diff] [blame] | 101 | |
Benjamin Peterson | 344ff4a | 2014-08-19 16:13:26 -0500 | [diff] [blame] | 102 | .. versionchanged:: 3.5 |
Yury Selivanov | f03d50c | 2015-09-09 09:32:07 -0400 | [diff] [blame] | 103 | Added the ``-j``, ``-r``, and ``-qq`` options. ``-q`` option |
Serhiy Storchaka | 2446eab | 2015-09-13 21:09:17 +0300 | [diff] [blame] | 104 | was changed to a multilevel value. ``-b`` will always produce a |
Yury Selivanov | f03d50c | 2015-09-09 09:32:07 -0400 | [diff] [blame] | 105 | byte-code file ending in ``.pyc``, never ``.pyo``. |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 106 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 107 | .. versionchanged:: 3.7 |
| 108 | Added the ``--invalidation-mode`` parameter. |
| 109 | |
Benjamin Peterson | 344ff4a | 2014-08-19 16:13:26 -0500 | [diff] [blame] | 110 | |
Éric Araujo | 01606de | 2011-03-26 03:22:55 +0100 | [diff] [blame] | 111 | There is no command-line option to control the optimization level used by the |
| 112 | :func:`compile` function, because the Python interpreter itself already |
| 113 | provides the option: :program:`python -O -m compileall`. |
Éric Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 114 | |
Carl Meyer | b193fa9 | 2018-06-15 22:40:56 -0600 | [diff] [blame] | 115 | Similarly, the :func:`compile` function respects the :attr:`sys.pycache_prefix` |
| 116 | setting. The generated bytecode cache will only be useful if :func:`compile` is |
| 117 | run with the same :attr:`sys.pycache_prefix` (if any) that will be used at |
| 118 | runtime. |
| 119 | |
Éric Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 120 | Public functions |
| 121 | ---------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 122 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 123 | .. 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 124 | |
| 125 | Recursively descend the directory tree named by *dir*, compiling all :file:`.py` |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 126 | files along the way. Return a true value if all the files compiled successfully, |
| 127 | and a false value otherwise. |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 128 | |
| 129 | The *maxlevels* parameter is used to limit the depth of the recursion; it |
| 130 | defaults to ``10``. |
| 131 | |
| 132 | If *ddir* is given, it is prepended to the path to each file being compiled |
| 133 | for use in compilation time tracebacks, and is also compiled in to the |
| 134 | byte-code file, where it will be used in tracebacks and other messages in |
| 135 | cases where the source file does not exist at the time the byte-code file is |
| 136 | executed. |
| 137 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 138 | If *force* is true, modules are re-compiled even if the timestamps are up to |
| 139 | date. |
| 140 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 141 | If *rx* is given, its search method is called on the complete path to each |
| 142 | file considered for compilation, and if it returns a true value, the file |
| 143 | is skipped. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 144 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 145 | If *quiet* is ``False`` or ``0`` (the default), the filenames and other |
| 146 | information are printed to standard out. Set to ``1``, only errors are |
| 147 | printed. Set to ``2``, all output is suppressed. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 148 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 149 | If *legacy* is true, byte-code files are written to their legacy locations |
| 150 | and names, which may overwrite byte-code files created by another version of |
| 151 | Python. The default is to write files to their :pep:`3147` locations and |
| 152 | names, which allows byte-code files from multiple versions of Python to |
| 153 | coexist. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 154 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 155 | *optimize* specifies the optimization level for the compiler. It is passed to |
| 156 | the built-in :func:`compile` function. |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 157 | |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 158 | The argument *workers* specifies how many workers are used to |
| 159 | compile files in parallel. The default is to not use multiple workers. |
| 160 | If the platform can't use multiple workers and *workers* argument is given, |
Berker Peksag | d86ef05 | 2015-04-22 09:39:19 +0300 | [diff] [blame] | 161 | then sequential compilation will be used as a fallback. If *workers* is |
| 162 | lower than ``0``, a :exc:`ValueError` will be raised. |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 163 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 164 | *invalidation_mode* should be a member of the |
| 165 | :class:`py_compile.PycInvalidationMode` enum and controls how the generated |
| 166 | pycs are invalidated at runtime. |
| 167 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 168 | .. versionchanged:: 3.2 |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 169 | Added the *legacy* and *optimize* parameter. |
| 170 | |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 171 | .. versionchanged:: 3.5 |
| 172 | Added the *workers* parameter. |
| 173 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 174 | .. versionchanged:: 3.5 |
| 175 | *quiet* parameter was changed to a multilevel value. |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 176 | |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 177 | .. versionchanged:: 3.5 |
| 178 | The *legacy* parameter only writes out ``.pyc`` files, not ``.pyo`` files |
| 179 | no matter what the value of *optimize* is. |
| 180 | |
Berker Peksag | 812a2b6 | 2016-10-01 00:54:18 +0300 | [diff] [blame] | 181 | .. versionchanged:: 3.6 |
| 182 | Accepts a :term:`path-like object`. |
| 183 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 184 | .. versionchanged:: 3.7 |
| 185 | The *invalidation_mode* parameter was added. |
| 186 | |
| 187 | .. function:: compile_file(fullname, ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1, invalidation_mode=py_compile.PycInvalidationMode.TIMESTAMP) |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 188 | |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 189 | Compile the file with path *fullname*. Return a true value if the file |
| 190 | compiled successfully, and a false value otherwise. |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 191 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 192 | If *ddir* is given, it is prepended to the path to the file being compiled |
| 193 | for use in compilation time tracebacks, and is also compiled in to the |
| 194 | byte-code file, where it will be used in tracebacks and other messages in |
| 195 | cases where the source file does not exist at the time the byte-code file is |
| 196 | executed. |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 197 | |
R. David Murray | 8b24aac | 2011-02-11 22:37:16 +0000 | [diff] [blame] | 198 | If *rx* is given, its search method is passed the full path name to the |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 199 | file being compiled, and if it returns a true value, the file is not |
| 200 | compiled and ``True`` is returned. |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 201 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 202 | If *quiet* is ``False`` or ``0`` (the default), the filenames and other |
| 203 | information are printed to standard out. Set to ``1``, only errors are |
| 204 | printed. Set to ``2``, all output is suppressed. |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 205 | |
| 206 | If *legacy* is true, byte-code files are written to their legacy locations |
| 207 | and names, which may overwrite byte-code files created by another version of |
| 208 | Python. The default is to write files to their :pep:`3147` locations and |
| 209 | names, which allows byte-code files from multiple versions of Python to |
| 210 | coexist. |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 211 | |
| 212 | *optimize* specifies the optimization level for the compiler. It is passed to |
| 213 | the built-in :func:`compile` function. |
| 214 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 215 | *invalidation_mode* should be a member of the |
| 216 | :class:`py_compile.PycInvalidationMode` enum and controls how the generated |
| 217 | pycs are invalidated at runtime. |
| 218 | |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 219 | .. versionadded:: 3.2 |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 220 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 221 | .. versionchanged:: 3.5 |
| 222 | *quiet* parameter was changed to a multilevel value. |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 223 | |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 224 | .. versionchanged:: 3.5 |
| 225 | The *legacy* parameter only writes out ``.pyc`` files, not ``.pyo`` files |
| 226 | no matter what the value of *optimize* is. |
| 227 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 228 | .. versionchanged:: 3.7 |
| 229 | The *invalidation_mode* parameter was added. |
| 230 | |
| 231 | .. function:: compile_path(skip_curdir=True, maxlevels=0, force=False, quiet=0, legacy=False, optimize=-1, invalidation_mode=py_compile.PycInvalidationMode.TIMESTAMP) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 232 | |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 233 | Byte-compile all the :file:`.py` files found along ``sys.path``. Return a |
| 234 | true value if all the files compiled successfully, and a false value otherwise. |
| 235 | |
| 236 | If *skip_curdir* is true (the default), the current directory is not included |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 237 | in the search. All other parameters are passed to the :func:`compile_dir` |
| 238 | function. Note that unlike the other compile functions, ``maxlevels`` |
| 239 | defaults to ``0``. |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 240 | |
| 241 | .. versionchanged:: 3.2 |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 242 | Added the *legacy* and *optimize* parameter. |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 243 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 244 | .. versionchanged:: 3.5 |
| 245 | *quiet* parameter was changed to a multilevel value. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 246 | |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 247 | .. versionchanged:: 3.5 |
| 248 | The *legacy* parameter only writes out ``.pyc`` files, not ``.pyo`` files |
| 249 | no matter what the value of *optimize* is. |
| 250 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 251 | .. versionchanged:: 3.7 |
| 252 | The *invalidation_mode* parameter was added. |
| 253 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 254 | To force a recompile of all the :file:`.py` files in the :file:`Lib/` |
| 255 | subdirectory and all its subdirectories:: |
| 256 | |
| 257 | import compileall |
| 258 | |
| 259 | compileall.compile_dir('Lib/', force=True) |
| 260 | |
| 261 | # Perform same compilation, excluding files in .svn directories. |
| 262 | import re |
Georg Brandl | 1aca953 | 2013-04-14 12:02:43 +0200 | [diff] [blame] | 263 | compileall.compile_dir('Lib/', rx=re.compile(r'[/\\][.]svn'), force=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 264 | |
Berker Peksag | 812a2b6 | 2016-10-01 00:54:18 +0300 | [diff] [blame] | 265 | # pathlib.Path objects can also be used. |
| 266 | import pathlib |
| 267 | compileall.compile_dir(pathlib.Path('Lib/'), force=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 268 | |
| 269 | .. seealso:: |
| 270 | |
| 271 | Module :mod:`py_compile` |
| 272 | Byte-compile a single source file. |