blob: d515d4d2fb178714f03629a070423897806b269d [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
7
8This module provides some utility functions to support installing Python
R. David Murray94f58c32010-12-17 16:29:07 +00009libraries. These functions compile Python source files in a directory tree.
10This module can be used to create the cached byte-code files at library
11installation time, which makes them available for use even by users who don't
12have write permission to the library directories.
Georg Brandl116aa622007-08-15 14:28:22 +000013
Georg Brandl116aa622007-08-15 14:28:22 +000014
Éric Araujo713d3032010-11-18 16:38:46 +000015Command-line use
16----------------
17
18This module can work as a script (using :program:`python -m compileall`) to
19compile Python sources.
20
21.. program:: compileall
22
23.. cmdoption:: [directory|file]...
24
25 Positional arguments are files to compile or directories that contain
26 source files, traversed recursively. If no argument is given, behave as if
27 the command line was ``-l <directories from sys.path>``.
28
29.. cmdoption:: -l
30
R. David Murray94f58c32010-12-17 16:29:07 +000031 Do not recurse into subdirectories, only compile source code files directly
32 contained in the named or implied directories.
Éric Araujo713d3032010-11-18 16:38:46 +000033
34.. cmdoption:: -f
35
36 Force rebuild even if timestamps are up-to-date.
37
38.. cmdoption:: -q
39
R. David Murray94f58c32010-12-17 16:29:07 +000040 Do not print the list of files compiled, print only error messages.
Éric Araujo713d3032010-11-18 16:38:46 +000041
42.. cmdoption:: -d destdir
43
R. David Murray94f58c32010-12-17 16:29:07 +000044 Directory prepended to the path to each file being compiled. This will
45 appear in compilation time tracebacks, and is also compiled in to the
46 byte-code file, where it will be used in tracebacks and other messages in
47 cases where the source file does not exist at the time the byte-code file is
48 executed.
Éric Araujo713d3032010-11-18 16:38:46 +000049
50.. cmdoption:: -x regex
51
R. David Murray94f58c32010-12-17 16:29:07 +000052 regex is used to search the full path to each file considered for
53 compilation, and if the regex produces a match, the file is skipped.
Éric Araujo713d3032010-11-18 16:38:46 +000054
55.. cmdoption:: -i list
56
R. David Murray94f58c32010-12-17 16:29:07 +000057 Read the file ``list`` and add each line that it contains to the list of
58 files and directories to compile. If ``list`` is ``-``, read lines from
59 ``stdin``.
Éric Araujo713d3032010-11-18 16:38:46 +000060
61.. cmdoption:: -b
62
R. David Murray94f58c32010-12-17 16:29:07 +000063 Write the byte-code files to their legacy locations and names, which may
64 overwrite byte-code files created by another version of Python. The default
65 is to write files to their :pep:`3147` locations and names, which allows
66 byte-code files from multiple versions of Python to coexist.
Éric Araujo713d3032010-11-18 16:38:46 +000067
Éric Araujo930df312010-12-16 06:28:48 +000068.. versionchanged:: 3.2
69 Added the ``-i``, ``-b`` and ``-h`` options.
Éric Araujof68fa052010-12-16 02:10:11 +000070
Éric Araujo713d3032010-11-18 16:38:46 +000071
72Public functions
73----------------
Georg Brandl116aa622007-08-15 14:28:22 +000074
Georg Brandl8334fd92010-12-04 10:26:46 +000075.. function:: compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None, quiet=False, legacy=False, optimize=-1)
Georg Brandl116aa622007-08-15 14:28:22 +000076
77 Recursively descend the directory tree named by *dir*, compiling all :file:`.py`
R. David Murray94f58c32010-12-17 16:29:07 +000078 files along the way.
79
80 The *maxlevels* parameter is used to limit the depth of the recursion; it
81 defaults to ``10``.
82
83 If *ddir* is given, it is prepended to the path to each file being compiled
84 for use in compilation time tracebacks, and is also compiled in to the
85 byte-code file, where it will be used in tracebacks and other messages in
86 cases where the source file does not exist at the time the byte-code file is
87 executed.
88
Georg Brandl116aa622007-08-15 14:28:22 +000089 If *force* is true, modules are re-compiled even if the timestamps are up to
90 date.
91
R. David Murray94f58c32010-12-17 16:29:07 +000092 If *rx* is given, its search method is called on the complete path to each
93 file considered for compilation, and if it returns a true value, the file
94 is skipped.
Georg Brandl116aa622007-08-15 14:28:22 +000095
R. David Murray94f58c32010-12-17 16:29:07 +000096 If *quiet* is true, nothing is printed to the standard output unless errors
97 occur.
Georg Brandl116aa622007-08-15 14:28:22 +000098
R. David Murray94f58c32010-12-17 16:29:07 +000099 If *legacy* is true, byte-code files are written to their legacy locations
100 and names, which may overwrite byte-code files created by another version of
101 Python. The default is to write files to their :pep:`3147` locations and
102 names, which allows byte-code files from multiple versions of Python to
103 coexist.
Georg Brandl116aa622007-08-15 14:28:22 +0000104
Georg Brandl8334fd92010-12-04 10:26:46 +0000105 *optimize* specifies the optimization level for the compiler. It is passed to
106 the built-in :func:`compile` function.
Barry Warsaw28a691b2010-04-17 00:19:56 +0000107
Georg Brandl8334fd92010-12-04 10:26:46 +0000108 .. versionchanged:: 3.2
Éric Araujo930df312010-12-16 06:28:48 +0000109 Added the *legacy* and *optimize* parameter.
110
111
112.. function:: compile_file(fullname, ddir=None, force=False, rx=None, quiet=False, legacy=False, optimize=-1)
113
R. David Murray94f58c32010-12-17 16:29:07 +0000114 Compile the file with path *fullname*.
Éric Araujo930df312010-12-16 06:28:48 +0000115
R. David Murray94f58c32010-12-17 16:29:07 +0000116 If *ddir* is given, it is prepended to the path to the 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.
Éric Araujo930df312010-12-16 06:28:48 +0000121
R. David Murray94f58c32010-12-17 16:29:07 +0000122 If *ra* is given, its search method is passed the full path name to the
123 file being compiled, and if it returns a true value, the file is not
124 compiled and ``True`` is returned.
Éric Araujo930df312010-12-16 06:28:48 +0000125
R. David Murray94f58c32010-12-17 16:29:07 +0000126 If *quiet* is true, nothing is printed to the standard output unless errors
127 occur.
128
129 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.
Éric Araujo930df312010-12-16 06:28:48 +0000134
135 *optimize* specifies the optimization level for the compiler. It is passed to
136 the built-in :func:`compile` function.
137
138 .. versionadded:: 3.2
Georg Brandl8334fd92010-12-04 10:26:46 +0000139
140
141.. function:: compile_path(skip_curdir=True, maxlevels=0, force=False, legacy=False, optimize=-1)
Georg Brandl116aa622007-08-15 14:28:22 +0000142
143 Byte-compile all the :file:`.py` files found along ``sys.path``. If
R. David Murray94f58c32010-12-17 16:29:07 +0000144 *skip_curdir* is true (the default), the current directory is not included
145 in the search. All other parameters are passed to the :func:`compile_dir`
146 function. Note that unlike the other compile functions, ``maxlevels``
147 defaults to ``0``.
Georg Brandl8334fd92010-12-04 10:26:46 +0000148
149 .. versionchanged:: 3.2
Éric Araujo930df312010-12-16 06:28:48 +0000150 Added the *legacy* and *optimize* parameter.
Georg Brandl8334fd92010-12-04 10:26:46 +0000151
Georg Brandl116aa622007-08-15 14:28:22 +0000152
153To force a recompile of all the :file:`.py` files in the :file:`Lib/`
154subdirectory and all its subdirectories::
155
156 import compileall
157
158 compileall.compile_dir('Lib/', force=True)
159
160 # Perform same compilation, excluding files in .svn directories.
161 import re
162 compileall.compile_dir('Lib/', rx=re.compile('/[.]svn'), force=True)
163
164
165.. seealso::
166
167 Module :mod:`py_compile`
168 Byte-compile a single source file.