blob: f805787e04759b3f7f1df7c33586e990ea64214e [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +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 Murray561b96f2011-02-11 17:25:54 +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 Brandl8ec7f652007-08-15 14:28:01 +000013
Georg Brandl8ec7f652007-08-15 14:28:01 +000014
Éric Araujoa8132ec2010-12-16 03:53:53 +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
Benjamin Petersonbeda1102014-09-04 22:40:34 -040023.. cmdoption:: directory ...
24 file ...
Éric Araujoa8132ec2010-12-16 03:53:53 +000025
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 Murray561b96f2011-02-11 17:25:54 +000032 Do not recurse into subdirectories, only compile source code files directly
33 contained in the named or implied directories.
Éric Araujoa8132ec2010-12-16 03:53:53 +000034
35.. cmdoption:: -f
36
37 Force rebuild even if timestamps are up-to-date.
38
39.. cmdoption:: -q
40
R. David Murray561b96f2011-02-11 17:25:54 +000041 Do not print the list of files compiled, print only error messages.
Éric Araujoa8132ec2010-12-16 03:53:53 +000042
43.. cmdoption:: -d destdir
44
R. David Murray561b96f2011-02-11 17:25:54 +000045 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 Araujoa8132ec2010-12-16 03:53:53 +000050
51.. cmdoption:: -x regex
52
R. David Murray561b96f2011-02-11 17:25:54 +000053 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 Araujoa8132ec2010-12-16 03:53:53 +000055
56.. cmdoption:: -i list
57
R. David Murray561b96f2011-02-11 17:25:54 +000058 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 Araujoa8132ec2010-12-16 03:53:53 +000061
Éric Araujoc11ba762010-12-16 06:15:02 +000062.. versionchanged:: 2.7
63 Added the ``-i`` option.
Éric Araujoa8132ec2010-12-16 03:53:53 +000064
65
66Public functions
67----------------
Georg Brandl8ec7f652007-08-15 14:28:01 +000068
Éric Araujoc11ba762010-12-16 06:15:02 +000069.. function:: compile_dir(dir[, maxlevels[, ddir[, force[, rx[, quiet]]]]])
Georg Brandl8ec7f652007-08-15 14:28:01 +000070
71 Recursively descend the directory tree named by *dir*, compiling all :file:`.py`
R. David Murray561b96f2011-02-11 17:25:54 +000072 files along the way.
73
74 The *maxlevels* parameter is used to limit the depth of the recursion; it
75 defaults to ``10``.
76
77 If *ddir* is given, it is prepended to the path to each file being compiled
78 for use in compilation time tracebacks, and is also compiled in to the
79 byte-code file, where it will be used in tracebacks and other messages in
80 cases where the source file does not exist at the time the byte-code file is
81 executed.
82
Georg Brandl8ec7f652007-08-15 14:28:01 +000083 If *force* is true, modules are re-compiled even if the timestamps are up to
84 date.
85
R. David Murray561b96f2011-02-11 17:25:54 +000086 If *rx* is given, its search method is called on the complete path to each
87 file considered for compilation, and if it returns a true value, the file
88 is skipped.
Georg Brandl8ec7f652007-08-15 14:28:01 +000089
R. David Murray561b96f2011-02-11 17:25:54 +000090 If *quiet* is true, nothing is printed to the standard output unless errors
91 occur.
Georg Brandl8ec7f652007-08-15 14:28:01 +000092
Éric Araujoc11ba762010-12-16 06:15:02 +000093
94.. function:: compile_file(fullname[, ddir[, force[, rx[, quiet]]]])
95
R. David Murray561b96f2011-02-11 17:25:54 +000096 Compile the file with path *fullname*.
Éric Araujoc11ba762010-12-16 06:15:02 +000097
R. David Murray561b96f2011-02-11 17:25:54 +000098 If *ddir* is given, it is prepended to the path to the file being compiled
99 for use in compilation time tracebacks, and is also compiled in to the
100 byte-code file, where it will be used in tracebacks and other messages in
101 cases where the source file does not exist at the time the byte-code file is
102 executed.
Éric Araujoc11ba762010-12-16 06:15:02 +0000103
R. David Murray46c4e472011-02-11 22:54:34 +0000104 If *rx* is given, its search method is passed the full path name to the
R. David Murray561b96f2011-02-11 17:25:54 +0000105 file being compiled, and if it returns a true value, the file is not
106 compiled and ``True`` is returned.
107
108 If *quiet* is true, nothing is printed to the standard output unless errors
109 occur.
Éric Araujoc11ba762010-12-16 06:15:02 +0000110
111 .. versionadded:: 2.7
112
113
Georg Brandl8ec7f652007-08-15 14:28:01 +0000114.. function:: compile_path([skip_curdir[, maxlevels[, force]]])
115
116 Byte-compile all the :file:`.py` files found along ``sys.path``. If
R. David Murray561b96f2011-02-11 17:25:54 +0000117 *skip_curdir* is true (the default), the current directory is not included
118 in the search. All other parameters are passed to the :func:`compile_dir`
119 function. Note that unlike the other compile functions, ``maxlevels``
120 defaults to ``0``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000121
122To force a recompile of all the :file:`.py` files in the :file:`Lib/`
123subdirectory and all its subdirectories::
124
125 import compileall
126
127 compileall.compile_dir('Lib/', force=True)
128
129 # Perform same compilation, excluding files in .svn directories.
130 import re
Georg Brandl95aa1722013-04-14 12:02:43 +0200131 compileall.compile_dir('Lib/', rx=re.compile(r'[/\\][.]svn'), force=True)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000132
133
134.. seealso::
135
136 Module :mod:`py_compile`
137 Byte-compile a single source file.