blob: cf0d5f85a0a383bb616c939f689b788297ee01ec [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
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 Murray561b96f2011-02-11 17:25:54 +000031 Do not recurse into subdirectories, only compile source code files directly
32 contained in the named or implied directories.
Éric Araujoa8132ec2010-12-16 03:53:53 +000033
34.. cmdoption:: -f
35
36 Force rebuild even if timestamps are up-to-date.
37
38.. cmdoption:: -q
39
R. David Murray561b96f2011-02-11 17:25:54 +000040 Do not print the list of files compiled, print only error messages.
Éric Araujoa8132ec2010-12-16 03:53:53 +000041
42.. cmdoption:: -d destdir
43
R. David Murray561b96f2011-02-11 17:25:54 +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 Araujoa8132ec2010-12-16 03:53:53 +000049
50.. cmdoption:: -x regex
51
R. David Murray561b96f2011-02-11 17:25:54 +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 Araujoa8132ec2010-12-16 03:53:53 +000054
55.. cmdoption:: -i list
56
R. David Murray561b96f2011-02-11 17:25:54 +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 Araujoa8132ec2010-12-16 03:53:53 +000060
Éric Araujoc11ba762010-12-16 06:15:02 +000061.. versionchanged:: 2.7
62 Added the ``-i`` option.
Éric Araujoa8132ec2010-12-16 03:53:53 +000063
64
65Public functions
66----------------
Georg Brandl8ec7f652007-08-15 14:28:01 +000067
Éric Araujoc11ba762010-12-16 06:15:02 +000068.. function:: compile_dir(dir[, maxlevels[, ddir[, force[, rx[, quiet]]]]])
Georg Brandl8ec7f652007-08-15 14:28:01 +000069
70 Recursively descend the directory tree named by *dir*, compiling all :file:`.py`
R. David Murray561b96f2011-02-11 17:25:54 +000071 files along the way.
72
73 The *maxlevels* parameter is used to limit the depth of the recursion; it
74 defaults to ``10``.
75
76 If *ddir* is given, it is prepended to the path to each file being compiled
77 for use in compilation time tracebacks, and is also compiled in to the
78 byte-code file, where it will be used in tracebacks and other messages in
79 cases where the source file does not exist at the time the byte-code file is
80 executed.
81
Georg Brandl8ec7f652007-08-15 14:28:01 +000082 If *force* is true, modules are re-compiled even if the timestamps are up to
83 date.
84
R. David Murray561b96f2011-02-11 17:25:54 +000085 If *rx* is given, its search method is called on the complete path to each
86 file considered for compilation, and if it returns a true value, the file
87 is skipped.
Georg Brandl8ec7f652007-08-15 14:28:01 +000088
R. David Murray561b96f2011-02-11 17:25:54 +000089 If *quiet* is true, nothing is printed to the standard output unless errors
90 occur.
Georg Brandl8ec7f652007-08-15 14:28:01 +000091
Éric Araujoc11ba762010-12-16 06:15:02 +000092
93.. function:: compile_file(fullname[, ddir[, force[, rx[, quiet]]]])
94
R. David Murray561b96f2011-02-11 17:25:54 +000095 Compile the file with path *fullname*.
Éric Araujoc11ba762010-12-16 06:15:02 +000096
R. David Murray561b96f2011-02-11 17:25:54 +000097 If *ddir* is given, it is prepended to the path to the file being compiled
98 for use in compilation time tracebacks, and is also compiled in to the
99 byte-code file, where it will be used in tracebacks and other messages in
100 cases where the source file does not exist at the time the byte-code file is
101 executed.
Éric Araujoc11ba762010-12-16 06:15:02 +0000102
R. David Murray46c4e472011-02-11 22:54:34 +0000103 If *rx* is given, its search method is passed the full path name to the
R. David Murray561b96f2011-02-11 17:25:54 +0000104 file being compiled, and if it returns a true value, the file is not
105 compiled and ``True`` is returned.
106
107 If *quiet* is true, nothing is printed to the standard output unless errors
108 occur.
Éric Araujoc11ba762010-12-16 06:15:02 +0000109
110 .. versionadded:: 2.7
111
112
Georg Brandl8ec7f652007-08-15 14:28:01 +0000113.. function:: compile_path([skip_curdir[, maxlevels[, force]]])
114
115 Byte-compile all the :file:`.py` files found along ``sys.path``. If
R. David Murray561b96f2011-02-11 17:25:54 +0000116 *skip_curdir* is true (the default), the current directory is not included
117 in the search. All other parameters are passed to the :func:`compile_dir`
118 function. Note that unlike the other compile functions, ``maxlevels``
119 defaults to ``0``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000120
121To force a recompile of all the :file:`.py` files in the :file:`Lib/`
122subdirectory and all its subdirectories::
123
124 import compileall
125
126 compileall.compile_dir('Lib/', force=True)
127
128 # Perform same compilation, excluding files in .svn directories.
129 import re
130 compileall.compile_dir('Lib/', rx=re.compile('/[.]svn'), force=True)
131
132
133.. seealso::
134
135 Module :mod:`py_compile`
136 Byte-compile a single source file.