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 | |
| 12 | This module provides some utility functions to support installing Python |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 13 | libraries. These functions compile Python source files in a directory tree. |
| 14 | This module can be used to create the cached byte-code files at library |
| 15 | installation time, which makes them available for use even by users who don't |
| 16 | have write permission to the library directories. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 18 | |
Éric Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 19 | Command-line use |
| 20 | ---------------- |
| 21 | |
| 22 | This module can work as a script (using :program:`python -m compileall`) to |
| 23 | compile Python sources. |
| 24 | |
| 25 | .. program:: compileall |
| 26 | |
Georg Brandl | f5c801f | 2014-03-18 07:44:07 +0100 | [diff] [blame] | 27 | .. cmdoption:: directory ... |
| 28 | file ... |
Éric Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 29 | |
| 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 Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 36 | Do not recurse into subdirectories, only compile source code files directly |
| 37 | contained in the named or implied directories. |
Éric Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 38 | |
| 39 | .. cmdoption:: -f |
| 40 | |
| 41 | Force rebuild even if timestamps are up-to-date. |
| 42 | |
| 43 | .. cmdoption:: -q |
| 44 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 45 | 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 Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 47 | |
| 48 | .. cmdoption:: -d destdir |
| 49 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 50 | 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 Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 55 | |
| 56 | .. cmdoption:: -x regex |
| 57 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 58 | 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 Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 60 | |
| 61 | .. cmdoption:: -i list |
| 62 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 63 | 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 Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 66 | |
| 67 | .. cmdoption:: -b |
| 68 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 69 | 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 Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 73 | |
Benjamin Peterson | 344ff4a | 2014-08-19 16:13:26 -0500 | [diff] [blame] | 74 | .. 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 Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 81 | .. 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 Peterson | 344ff4a | 2014-08-19 16:13:26 -0500 | [diff] [blame] | 86 | |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 87 | .. versionchanged:: 3.2 |
| 88 | Added the ``-i``, ``-b`` and ``-h`` options. |
Éric Araujo | f68fa05 | 2010-12-16 02:10:11 +0000 | [diff] [blame] | 89 | |
Benjamin Peterson | 344ff4a | 2014-08-19 16:13:26 -0500 | [diff] [blame] | 90 | .. versionchanged:: 3.5 |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 91 | Added the ``-j`` and ``-r`` options. |
| 92 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 93 | .. versionchanged:: 3.5 |
| 94 | ``-q`` option was changed to a multilevel value. |
| 95 | |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 96 | .. versionchanged:: 3.5 |
| 97 | ``-b`` will always produce a byte-code file ending in ``.pyc``, never |
| 98 | ``.pyo``. |
| 99 | |
Benjamin Peterson | 344ff4a | 2014-08-19 16:13:26 -0500 | [diff] [blame] | 100 | |
Éric Araujo | 01606de | 2011-03-26 03:22:55 +0100 | [diff] [blame] | 101 | There is no command-line option to control the optimization level used by the |
| 102 | :func:`compile` function, because the Python interpreter itself already |
| 103 | provides the option: :program:`python -O -m compileall`. |
Éric Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 104 | |
| 105 | Public functions |
| 106 | ---------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 107 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 108 | .. function:: compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1, workers=1) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 109 | |
| 110 | Recursively descend the directory tree named by *dir*, compiling all :file:`.py` |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 111 | files along the way. |
| 112 | |
| 113 | The *maxlevels* parameter is used to limit the depth of the recursion; it |
| 114 | defaults to ``10``. |
| 115 | |
| 116 | If *ddir* is given, it is prepended to the path to each file being compiled |
| 117 | for use in compilation time tracebacks, and is also compiled in to the |
| 118 | byte-code file, where it will be used in tracebacks and other messages in |
| 119 | cases where the source file does not exist at the time the byte-code file is |
| 120 | executed. |
| 121 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 122 | If *force* is true, modules are re-compiled even if the timestamps are up to |
| 123 | date. |
| 124 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 125 | If *rx* is given, its search method is called on the complete path to each |
| 126 | file considered for compilation, and if it returns a true value, the file |
| 127 | is skipped. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 128 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 129 | If *quiet* is ``False`` or ``0`` (the default), the filenames and other |
| 130 | information are printed to standard out. Set to ``1``, only errors are |
| 131 | printed. Set to ``2``, all output is suppressed. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 132 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 133 | If *legacy* is true, byte-code files are written to their legacy locations |
| 134 | and names, which may overwrite byte-code files created by another version of |
| 135 | Python. The default is to write files to their :pep:`3147` locations and |
| 136 | names, which allows byte-code files from multiple versions of Python to |
| 137 | coexist. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 138 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 139 | *optimize* specifies the optimization level for the compiler. It is passed to |
| 140 | the built-in :func:`compile` function. |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 141 | |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 142 | The argument *workers* specifies how many workers are used to |
| 143 | compile files in parallel. The default is to not use multiple workers. |
| 144 | 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] | 145 | then sequential compilation will be used as a fallback. If *workers* is |
| 146 | lower than ``0``, a :exc:`ValueError` will be raised. |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 147 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 148 | .. versionchanged:: 3.2 |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 149 | Added the *legacy* and *optimize* parameter. |
| 150 | |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 151 | .. versionchanged:: 3.5 |
| 152 | Added the *workers* parameter. |
| 153 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 154 | .. versionchanged:: 3.5 |
| 155 | *quiet* parameter was changed to a multilevel value. |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 156 | |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 157 | .. versionchanged:: 3.5 |
| 158 | The *legacy* parameter only writes out ``.pyc`` files, not ``.pyo`` files |
| 159 | no matter what the value of *optimize* is. |
| 160 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 161 | .. function:: compile_file(fullname, ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1) |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 162 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 163 | Compile the file with path *fullname*. |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 164 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 165 | If *ddir* is given, it is prepended to the path to the file being compiled |
| 166 | for use in compilation time tracebacks, and is also compiled in to the |
| 167 | byte-code file, where it will be used in tracebacks and other messages in |
| 168 | cases where the source file does not exist at the time the byte-code file is |
| 169 | executed. |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 170 | |
R. David Murray | 8b24aac | 2011-02-11 22:37:16 +0000 | [diff] [blame] | 171 | 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] | 172 | file being compiled, and if it returns a true value, the file is not |
| 173 | compiled and ``True`` is returned. |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 174 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 175 | If *quiet* is ``False`` or ``0`` (the default), the filenames and other |
| 176 | information are printed to standard out. Set to ``1``, only errors are |
| 177 | printed. Set to ``2``, all output is suppressed. |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 178 | |
| 179 | If *legacy* is true, byte-code files are written to their legacy locations |
| 180 | and names, which may overwrite byte-code files created by another version of |
| 181 | Python. The default is to write files to their :pep:`3147` locations and |
| 182 | names, which allows byte-code files from multiple versions of Python to |
| 183 | coexist. |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 184 | |
| 185 | *optimize* specifies the optimization level for the compiler. It is passed to |
| 186 | the built-in :func:`compile` function. |
| 187 | |
| 188 | .. versionadded:: 3.2 |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 189 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 190 | .. versionchanged:: 3.5 |
| 191 | *quiet* parameter was changed to a multilevel value. |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 192 | |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 193 | .. versionchanged:: 3.5 |
| 194 | The *legacy* parameter only writes out ``.pyc`` files, not ``.pyo`` files |
| 195 | no matter what the value of *optimize* is. |
| 196 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 197 | .. function:: compile_path(skip_curdir=True, maxlevels=0, force=False, quiet=0, legacy=False, optimize=-1) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 198 | |
| 199 | Byte-compile all the :file:`.py` files found along ``sys.path``. If |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 200 | *skip_curdir* is true (the default), the current directory is not included |
| 201 | in the search. All other parameters are passed to the :func:`compile_dir` |
| 202 | function. Note that unlike the other compile functions, ``maxlevels`` |
| 203 | defaults to ``0``. |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 204 | |
| 205 | .. versionchanged:: 3.2 |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 206 | Added the *legacy* and *optimize* parameter. |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 207 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 208 | .. versionchanged:: 3.5 |
| 209 | *quiet* parameter was changed to a multilevel value. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 210 | |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 211 | .. versionchanged:: 3.5 |
| 212 | The *legacy* parameter only writes out ``.pyc`` files, not ``.pyo`` files |
| 213 | no matter what the value of *optimize* is. |
| 214 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 215 | To force a recompile of all the :file:`.py` files in the :file:`Lib/` |
| 216 | subdirectory and all its subdirectories:: |
| 217 | |
| 218 | import compileall |
| 219 | |
| 220 | compileall.compile_dir('Lib/', force=True) |
| 221 | |
| 222 | # Perform same compilation, excluding files in .svn directories. |
| 223 | import re |
Georg Brandl | 1aca953 | 2013-04-14 12:02:43 +0200 | [diff] [blame] | 224 | compileall.compile_dir('Lib/', rx=re.compile(r'[/\\][.]svn'), force=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 225 | |
| 226 | |
| 227 | .. seealso:: |
| 228 | |
| 229 | Module :mod:`py_compile` |
| 230 | Byte-compile a single source file. |