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 | |
| 7 | |
| 8 | This module provides some utility functions to support installing Python |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 9 | libraries. These functions compile Python source files in a directory tree. |
| 10 | This module can be used to create the cached byte-code files at library |
| 11 | installation time, which makes them available for use even by users who don't |
| 12 | have write permission to the library directories. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 14 | |
Éric Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 15 | Command-line use |
| 16 | ---------------- |
| 17 | |
| 18 | This module can work as a script (using :program:`python -m compileall`) to |
| 19 | compile Python sources. |
| 20 | |
| 21 | .. program:: compileall |
| 22 | |
Georg Brandl | f5c801f | 2014-03-18 07:44:07 +0100 | [diff] [blame] | 23 | .. cmdoption:: directory ... |
| 24 | file ... |
Éric Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 25 | |
| 26 | Positional arguments are files to compile or directories that contain |
| 27 | source files, traversed recursively. If no argument is given, behave as if |
| 28 | the command line was ``-l <directories from sys.path>``. |
| 29 | |
| 30 | .. cmdoption:: -l |
| 31 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 32 | Do not recurse into subdirectories, only compile source code files directly |
| 33 | contained in the named or implied directories. |
Éric Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 34 | |
| 35 | .. cmdoption:: -f |
| 36 | |
| 37 | Force rebuild even if timestamps are up-to-date. |
| 38 | |
| 39 | .. cmdoption:: -q |
| 40 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 41 | Do not print the list of files compiled, print only error messages. |
Éric Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 42 | |
| 43 | .. cmdoption:: -d destdir |
| 44 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 45 | Directory prepended to the path to each file being compiled. This will |
| 46 | appear in compilation time tracebacks, and is also compiled in to the |
| 47 | byte-code file, where it will be used in tracebacks and other messages in |
| 48 | cases where the source file does not exist at the time the byte-code file is |
| 49 | executed. |
Éric Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 50 | |
| 51 | .. cmdoption:: -x regex |
| 52 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 53 | regex is used to search the full path to each file considered for |
| 54 | compilation, and if the regex produces a match, the file is skipped. |
Éric Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 55 | |
| 56 | .. cmdoption:: -i list |
| 57 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 58 | Read the file ``list`` and add each line that it contains to the list of |
| 59 | files and directories to compile. If ``list`` is ``-``, read lines from |
| 60 | ``stdin``. |
Éric Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 61 | |
| 62 | .. cmdoption:: -b |
| 63 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 64 | Write the byte-code files to their legacy locations and names, which may |
| 65 | overwrite byte-code files created by another version of Python. The default |
| 66 | is to write files to their :pep:`3147` locations and names, which allows |
| 67 | byte-code files from multiple versions of Python to coexist. |
Éric Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 68 | |
Benjamin Peterson | 344ff4a | 2014-08-19 16:13:26 -0500 | [diff] [blame] | 69 | .. cmdoption:: -r |
| 70 | |
| 71 | Control the maximum recursion level for subdirectories. |
| 72 | If this is given, then ``-l`` option will not be taken into account. |
| 73 | :program:`python -m compileall <directory> -r 0` is equivalent to |
| 74 | :program:`python -m compileall <directory> -l`. |
| 75 | |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 76 | .. cmdoption:: -j N |
| 77 | |
| 78 | Use *N* workers to compile the files within the given directory. |
| 79 | If ``0`` is used, then the result of :func:`os.cpu_count()` |
| 80 | will be used. |
Benjamin Peterson | 344ff4a | 2014-08-19 16:13:26 -0500 | [diff] [blame] | 81 | |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 82 | .. versionchanged:: 3.2 |
| 83 | Added the ``-i``, ``-b`` and ``-h`` options. |
Éric Araujo | f68fa05 | 2010-12-16 02:10:11 +0000 | [diff] [blame] | 84 | |
Benjamin Peterson | 344ff4a | 2014-08-19 16:13:26 -0500 | [diff] [blame] | 85 | .. versionchanged:: 3.5 |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 86 | Added the ``-j`` and ``-r`` options. |
| 87 | |
Benjamin Peterson | 344ff4a | 2014-08-19 16:13:26 -0500 | [diff] [blame] | 88 | |
Éric Araujo | 01606de | 2011-03-26 03:22:55 +0100 | [diff] [blame] | 89 | There is no command-line option to control the optimization level used by the |
| 90 | :func:`compile` function, because the Python interpreter itself already |
| 91 | provides the option: :program:`python -O -m compileall`. |
Éric Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 92 | |
| 93 | Public functions |
| 94 | ---------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 95 | |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 96 | .. function:: compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None, quiet=False, legacy=False, optimize=-1, workers=1) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 97 | |
| 98 | 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] | 99 | files along the way. |
| 100 | |
| 101 | The *maxlevels* parameter is used to limit the depth of the recursion; it |
| 102 | defaults to ``10``. |
| 103 | |
| 104 | If *ddir* is given, it is prepended to the path to each file being compiled |
| 105 | for use in compilation time tracebacks, and is also compiled in to the |
| 106 | byte-code file, where it will be used in tracebacks and other messages in |
| 107 | cases where the source file does not exist at the time the byte-code file is |
| 108 | executed. |
| 109 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 110 | If *force* is true, modules are re-compiled even if the timestamps are up to |
| 111 | date. |
| 112 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 113 | If *rx* is given, its search method is called on the complete path to each |
| 114 | file considered for compilation, and if it returns a true value, the file |
| 115 | is skipped. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 116 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 117 | If *quiet* is true, nothing is printed to the standard output unless errors |
| 118 | occur. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 119 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 120 | If *legacy* is true, byte-code files are written to their legacy locations |
| 121 | and names, which may overwrite byte-code files created by another version of |
| 122 | Python. The default is to write files to their :pep:`3147` locations and |
| 123 | names, which allows byte-code files from multiple versions of Python to |
| 124 | coexist. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 125 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 126 | *optimize* specifies the optimization level for the compiler. It is passed to |
| 127 | the built-in :func:`compile` function. |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 128 | |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 129 | The argument *workers* specifies how many workers are used to |
| 130 | compile files in parallel. The default is to not use multiple workers. |
| 131 | If the platform can't use multiple workers and *workers* argument is given, |
| 132 | then a :exc:`NotImplementedError` will be raised. |
| 133 | If *workers* is lower than ``0``, a :exc:`ValueError` will be raised. |
| 134 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 135 | .. versionchanged:: 3.2 |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 136 | Added the *legacy* and *optimize* parameter. |
| 137 | |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 138 | .. versionchanged:: 3.5 |
| 139 | Added the *workers* parameter. |
| 140 | |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 141 | |
| 142 | .. function:: compile_file(fullname, ddir=None, force=False, rx=None, quiet=False, legacy=False, optimize=-1) |
| 143 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 144 | Compile the file with path *fullname*. |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 145 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 146 | If *ddir* is given, it is prepended to the path to the file being compiled |
| 147 | for use in compilation time tracebacks, and is also compiled in to the |
| 148 | byte-code file, where it will be used in tracebacks and other messages in |
| 149 | cases where the source file does not exist at the time the byte-code file is |
| 150 | executed. |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 151 | |
R. David Murray | 8b24aac | 2011-02-11 22:37:16 +0000 | [diff] [blame] | 152 | 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] | 153 | file being compiled, and if it returns a true value, the file is not |
| 154 | compiled and ``True`` is returned. |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 155 | |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 156 | If *quiet* is true, nothing is printed to the standard output unless errors |
| 157 | occur. |
| 158 | |
| 159 | If *legacy* is true, byte-code files are written to their legacy locations |
| 160 | and names, which may overwrite byte-code files created by another version of |
| 161 | Python. The default is to write files to their :pep:`3147` locations and |
| 162 | names, which allows byte-code files from multiple versions of Python to |
| 163 | coexist. |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 164 | |
| 165 | *optimize* specifies the optimization level for the compiler. It is passed to |
| 166 | the built-in :func:`compile` function. |
| 167 | |
| 168 | .. versionadded:: 3.2 |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 169 | |
| 170 | |
| 171 | .. function:: compile_path(skip_curdir=True, maxlevels=0, force=False, legacy=False, optimize=-1) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 172 | |
| 173 | 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] | 174 | *skip_curdir* is true (the default), the current directory is not included |
| 175 | in the search. All other parameters are passed to the :func:`compile_dir` |
| 176 | function. Note that unlike the other compile functions, ``maxlevels`` |
| 177 | defaults to ``0``. |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 178 | |
| 179 | .. versionchanged:: 3.2 |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 180 | Added the *legacy* and *optimize* parameter. |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 181 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 182 | |
| 183 | To force a recompile of all the :file:`.py` files in the :file:`Lib/` |
| 184 | subdirectory and all its subdirectories:: |
| 185 | |
| 186 | import compileall |
| 187 | |
| 188 | compileall.compile_dir('Lib/', force=True) |
| 189 | |
| 190 | # Perform same compilation, excluding files in .svn directories. |
| 191 | import re |
Georg Brandl | 1aca953 | 2013-04-14 12:02:43 +0200 | [diff] [blame] | 192 | compileall.compile_dir('Lib/', rx=re.compile(r'[/\\][.]svn'), force=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 193 | |
| 194 | |
| 195 | .. seealso:: |
| 196 | |
| 197 | Module :mod:`py_compile` |
| 198 | Byte-compile a single source file. |