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 | |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 55 | .. 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 Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 62 | .. cmdoption:: -x regex |
| 63 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 64 | 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 Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 66 | |
| 67 | .. cmdoption:: -i list |
| 68 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 69 | 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 Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 72 | |
| 73 | .. cmdoption:: -b |
| 74 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 75 | 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 Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 79 | |
Benjamin Peterson | 344ff4a | 2014-08-19 16:13:26 -0500 | [diff] [blame] | 80 | .. 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 Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 87 | .. 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 Peterson | 344ff4a | 2014-08-19 16:13:26 -0500 | [diff] [blame] | 92 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 93 | .. cmdoption:: --invalidation-mode [timestamp|checked-hash|unchecked-hash] |
| 94 | |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 95 | Control how the generated byte-code files are invalidated at runtime. |
| 96 | The ``timestamp`` value, means that ``.pyc`` files with the source timestamp |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 97 | 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 Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 100 | :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 Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 105 | |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 106 | .. 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 | |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 116 | .. versionchanged:: 3.2 |
| 117 | Added the ``-i``, ``-b`` and ``-h`` options. |
Éric Araujo | f68fa05 | 2010-12-16 02:10:11 +0000 | [diff] [blame] | 118 | |
Benjamin Peterson | 344ff4a | 2014-08-19 16:13:26 -0500 | [diff] [blame] | 119 | .. versionchanged:: 3.5 |
Yury Selivanov | f03d50c | 2015-09-09 09:32:07 -0400 | [diff] [blame] | 120 | Added the ``-j``, ``-r``, and ``-qq`` options. ``-q`` option |
Serhiy Storchaka | 2446eab | 2015-09-13 21:09:17 +0300 | [diff] [blame] | 121 | was changed to a multilevel value. ``-b`` will always produce a |
Yury Selivanov | f03d50c | 2015-09-09 09:32:07 -0400 | [diff] [blame] | 122 | byte-code file ending in ``.pyc``, never ``.pyo``. |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 123 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 124 | .. versionchanged:: 3.7 |
NAKAMURA Osamu | 0983fcd | 2019-03-05 13:43:43 +0900 | [diff] [blame] | 125 | Added the ``--invalidation-mode`` option. |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 126 | |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 127 | .. versionchanged:: 3.9 |
| 128 | Added the ``-s``, ``-p``, ``-e`` options. |
| 129 | Raised the default recursion limit from 10 to |
| 130 | :py:func:`sys.getrecursionlimit()`. |
| 131 | Added the possibility to specify the ``-o`` option multiple times. |
| 132 | |
Benjamin Peterson | 344ff4a | 2014-08-19 16:13:26 -0500 | [diff] [blame] | 133 | |
Éric Araujo | 01606de | 2011-03-26 03:22:55 +0100 | [diff] [blame] | 134 | There is no command-line option to control the optimization level used by the |
| 135 | :func:`compile` function, because the Python interpreter itself already |
| 136 | provides the option: :program:`python -O -m compileall`. |
Éric Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 137 | |
Carl Meyer | b193fa9 | 2018-06-15 22:40:56 -0600 | [diff] [blame] | 138 | Similarly, the :func:`compile` function respects the :attr:`sys.pycache_prefix` |
| 139 | setting. The generated bytecode cache will only be useful if :func:`compile` is |
| 140 | run with the same :attr:`sys.pycache_prefix` (if any) that will be used at |
| 141 | runtime. |
| 142 | |
Éric Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 143 | Public functions |
| 144 | ---------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 145 | |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 146 | .. 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) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 147 | |
| 148 | Recursively descend the directory tree named by *dir*, compiling all :file:`.py` |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 149 | files along the way. Return a true value if all the files compiled successfully, |
| 150 | and a false value otherwise. |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 151 | |
| 152 | The *maxlevels* parameter is used to limit the depth of the recursion; it |
| 153 | defaults to ``10``. |
| 154 | |
| 155 | If *ddir* is given, it is prepended to the path to each file being compiled |
| 156 | for use in compilation time tracebacks, and is also compiled in to the |
| 157 | byte-code file, where it will be used in tracebacks and other messages in |
| 158 | cases where the source file does not exist at the time the byte-code file is |
| 159 | executed. |
| 160 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 161 | If *force* is true, modules are re-compiled even if the timestamps are up to |
| 162 | date. |
| 163 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 164 | If *rx* is given, its search method is called on the complete path to each |
| 165 | file considered for compilation, and if it returns a true value, the file |
| 166 | is skipped. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 167 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 168 | If *quiet* is ``False`` or ``0`` (the default), the filenames and other |
| 169 | information are printed to standard out. Set to ``1``, only errors are |
| 170 | printed. Set to ``2``, all output is suppressed. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 171 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 172 | If *legacy* is true, byte-code files are written to their legacy locations |
| 173 | and names, which may overwrite byte-code files created by another version of |
| 174 | Python. The default is to write files to their :pep:`3147` locations and |
| 175 | names, which allows byte-code files from multiple versions of Python to |
| 176 | coexist. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 177 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 178 | *optimize* specifies the optimization level for the compiler. It is passed to |
| 179 | the built-in :func:`compile` function. |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 180 | |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 181 | The argument *workers* specifies how many workers are used to |
| 182 | compile files in parallel. The default is to not use multiple workers. |
| 183 | If the platform can't use multiple workers and *workers* argument is given, |
Antoine Pitrou | 1a2dd82 | 2019-05-15 23:45:18 +0200 | [diff] [blame] | 184 | then sequential compilation will be used as a fallback. If *workers* |
| 185 | is 0, the number of cores in the system is used. If *workers* is |
Berker Peksag | d86ef05 | 2015-04-22 09:39:19 +0300 | [diff] [blame] | 186 | lower than ``0``, a :exc:`ValueError` will be raised. |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 187 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 188 | *invalidation_mode* should be a member of the |
| 189 | :class:`py_compile.PycInvalidationMode` enum and controls how the generated |
| 190 | pycs are invalidated at runtime. |
| 191 | |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 192 | The *stripdir*, *prependdir* and *limit_sl_dest* arguments correspond to |
| 193 | the ``-s``, ``-p`` and ``-e`` options described above. |
| 194 | They may be specified as ``str``, ``bytes`` or :py:class:`os.PathLike`. |
| 195 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 196 | .. versionchanged:: 3.2 |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 197 | Added the *legacy* and *optimize* parameter. |
| 198 | |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 199 | .. versionchanged:: 3.5 |
| 200 | Added the *workers* parameter. |
| 201 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 202 | .. versionchanged:: 3.5 |
| 203 | *quiet* parameter was changed to a multilevel value. |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 204 | |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 205 | .. versionchanged:: 3.5 |
| 206 | The *legacy* parameter only writes out ``.pyc`` files, not ``.pyo`` files |
| 207 | no matter what the value of *optimize* is. |
| 208 | |
Berker Peksag | 812a2b6 | 2016-10-01 00:54:18 +0300 | [diff] [blame] | 209 | .. versionchanged:: 3.6 |
| 210 | Accepts a :term:`path-like object`. |
| 211 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 212 | .. versionchanged:: 3.7 |
| 213 | The *invalidation_mode* parameter was added. |
| 214 | |
Hai Shi | 68e495d | 2019-08-14 17:03:11 -0500 | [diff] [blame] | 215 | .. versionchanged:: 3.7.2 |
| 216 | The *invalidation_mode* parameter's default value is updated to None. |
| 217 | |
Antoine Pitrou | 1a2dd82 | 2019-05-15 23:45:18 +0200 | [diff] [blame] | 218 | .. versionchanged:: 3.8 |
| 219 | Setting *workers* to 0 now chooses the optimal number of cores. |
| 220 | |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 221 | .. versionchanged:: 3.9 |
| 222 | Added *stripdir*, *prependdir* and *limit_sl_dest* arguments. |
| 223 | |
Hai Shi | 68e495d | 2019-08-14 17:03:11 -0500 | [diff] [blame] | 224 | .. function:: compile_file(fullname, ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1, invalidation_mode=None) |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 225 | |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 226 | Compile the file with path *fullname*. Return a true value if the file |
| 227 | compiled successfully, and a false value otherwise. |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 228 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 229 | If *ddir* is given, it is prepended to the path to the file being compiled |
| 230 | for use in compilation time tracebacks, and is also compiled in to the |
| 231 | byte-code file, where it will be used in tracebacks and other messages in |
| 232 | cases where the source file does not exist at the time the byte-code file is |
| 233 | executed. |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 234 | |
R. David Murray | 8b24aac | 2011-02-11 22:37:16 +0000 | [diff] [blame] | 235 | 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] | 236 | file being compiled, and if it returns a true value, the file is not |
| 237 | compiled and ``True`` is returned. |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 238 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 239 | If *quiet* is ``False`` or ``0`` (the default), the filenames and other |
| 240 | information are printed to standard out. Set to ``1``, only errors are |
| 241 | printed. Set to ``2``, all output is suppressed. |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 242 | |
| 243 | If *legacy* is true, byte-code files are written to their legacy locations |
| 244 | and names, which may overwrite byte-code files created by another version of |
| 245 | Python. The default is to write files to their :pep:`3147` locations and |
| 246 | names, which allows byte-code files from multiple versions of Python to |
| 247 | coexist. |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 248 | |
| 249 | *optimize* specifies the optimization level for the compiler. It is passed to |
| 250 | the built-in :func:`compile` function. |
| 251 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 252 | *invalidation_mode* should be a member of the |
| 253 | :class:`py_compile.PycInvalidationMode` enum and controls how the generated |
| 254 | pycs are invalidated at runtime. |
| 255 | |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 256 | The *stripdir*, *prependdir* and *limit_sl_dest* arguments correspond to |
| 257 | the ``-s``, ``-p`` and ``-e`` options described above. |
| 258 | They may be specified as ``str``, ``bytes`` or :py:class:`os.PathLike`. |
| 259 | |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 260 | .. versionadded:: 3.2 |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 261 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 262 | .. versionchanged:: 3.5 |
| 263 | *quiet* parameter was changed to a multilevel value. |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 264 | |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 265 | .. versionchanged:: 3.5 |
| 266 | The *legacy* parameter only writes out ``.pyc`` files, not ``.pyo`` files |
| 267 | no matter what the value of *optimize* is. |
| 268 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 269 | .. versionchanged:: 3.7 |
| 270 | The *invalidation_mode* parameter was added. |
| 271 | |
Hai Shi | 68e495d | 2019-08-14 17:03:11 -0500 | [diff] [blame] | 272 | .. versionchanged:: 3.7.2 |
| 273 | The *invalidation_mode* parameter's default value is updated to None. |
| 274 | |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 275 | .. versionchanged:: 3.9 |
| 276 | Added *stripdir*, *prependdir* and *limit_sl_dest* arguments. |
| 277 | |
Hai Shi | 68e495d | 2019-08-14 17:03:11 -0500 | [diff] [blame] | 278 | .. function:: compile_path(skip_curdir=True, maxlevels=0, force=False, quiet=0, legacy=False, optimize=-1, invalidation_mode=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 279 | |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 280 | Byte-compile all the :file:`.py` files found along ``sys.path``. Return a |
| 281 | true value if all the files compiled successfully, and a false value otherwise. |
| 282 | |
| 283 | 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] | 284 | in the search. All other parameters are passed to the :func:`compile_dir` |
| 285 | function. Note that unlike the other compile functions, ``maxlevels`` |
| 286 | defaults to ``0``. |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 287 | |
| 288 | .. versionchanged:: 3.2 |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 289 | Added the *legacy* and *optimize* parameter. |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 290 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 291 | .. versionchanged:: 3.5 |
| 292 | *quiet* parameter was changed to a multilevel value. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 293 | |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 294 | .. versionchanged:: 3.5 |
| 295 | The *legacy* parameter only writes out ``.pyc`` files, not ``.pyo`` files |
| 296 | no matter what the value of *optimize* is. |
| 297 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 298 | .. versionchanged:: 3.7 |
| 299 | The *invalidation_mode* parameter was added. |
| 300 | |
Hai Shi | 68e495d | 2019-08-14 17:03:11 -0500 | [diff] [blame] | 301 | .. versionchanged:: 3.7.2 |
| 302 | The *invalidation_mode* parameter's default value is updated to None. |
| 303 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 304 | To force a recompile of all the :file:`.py` files in the :file:`Lib/` |
| 305 | subdirectory and all its subdirectories:: |
| 306 | |
| 307 | import compileall |
| 308 | |
| 309 | compileall.compile_dir('Lib/', force=True) |
| 310 | |
| 311 | # Perform same compilation, excluding files in .svn directories. |
| 312 | import re |
Georg Brandl | 1aca953 | 2013-04-14 12:02:43 +0200 | [diff] [blame] | 313 | compileall.compile_dir('Lib/', rx=re.compile(r'[/\\][.]svn'), force=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 314 | |
Berker Peksag | 812a2b6 | 2016-10-01 00:54:18 +0300 | [diff] [blame] | 315 | # pathlib.Path objects can also be used. |
| 316 | import pathlib |
| 317 | compileall.compile_dir(pathlib.Path('Lib/'), force=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 318 | |
| 319 | .. seealso:: |
| 320 | |
| 321 | Module :mod:`py_compile` |
| 322 | Byte-compile a single source file. |