blob: 9712de2fef3463ae60a1cc8ac1b4cf50847b9eb2 [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
Georg Brandlc0a8f8c2014-10-02 08:38:39 +02007**Source code:** :source:`Lib/compileall.py`
8
9--------------
10
Georg Brandl116aa622007-08-15 14:28:22 +000011
12This module provides some utility functions to support installing Python
R. David Murray94f58c32010-12-17 16:29:07 +000013libraries. These functions compile Python source files in a directory tree.
14This module can be used to create the cached byte-code files at library
15installation time, which makes them available for use even by users who don't
16have write permission to the library directories.
Georg Brandl116aa622007-08-15 14:28:22 +000017
Georg Brandl116aa622007-08-15 14:28:22 +000018
Éric Araujo713d3032010-11-18 16:38:46 +000019Command-line use
20----------------
21
22This module can work as a script (using :program:`python -m compileall`) to
23compile Python sources.
24
25.. program:: compileall
26
Georg Brandlf5c801f2014-03-18 07:44:07 +010027.. cmdoption:: directory ...
28 file ...
Éric Araujo713d3032010-11-18 16:38:46 +000029
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 Murray94f58c32010-12-17 16:29:07 +000036 Do not recurse into subdirectories, only compile source code files directly
37 contained in the named or implied directories.
Éric Araujo713d3032010-11-18 16:38:46 +000038
39.. cmdoption:: -f
40
41 Force rebuild even if timestamps are up-to-date.
42
43.. cmdoption:: -q
44
R. David Murray94f58c32010-12-17 16:29:07 +000045 Do not print the list of files compiled, print only error messages.
Éric Araujo713d3032010-11-18 16:38:46 +000046
47.. cmdoption:: -d destdir
48
R. David Murray94f58c32010-12-17 16:29:07 +000049 Directory prepended to the path to each file being compiled. This will
50 appear in compilation time tracebacks, and is also compiled in to the
51 byte-code file, where it will be used in tracebacks and other messages in
52 cases where the source file does not exist at the time the byte-code file is
53 executed.
Éric Araujo713d3032010-11-18 16:38:46 +000054
55.. cmdoption:: -x regex
56
R. David Murray94f58c32010-12-17 16:29:07 +000057 regex is used to search the full path to each file considered for
58 compilation, and if the regex produces a match, the file is skipped.
Éric Araujo713d3032010-11-18 16:38:46 +000059
60.. cmdoption:: -i list
61
R. David Murray94f58c32010-12-17 16:29:07 +000062 Read the file ``list`` and add each line that it contains to the list of
63 files and directories to compile. If ``list`` is ``-``, read lines from
64 ``stdin``.
Éric Araujo713d3032010-11-18 16:38:46 +000065
66.. cmdoption:: -b
67
R. David Murray94f58c32010-12-17 16:29:07 +000068 Write the byte-code files to their legacy locations and names, which may
69 overwrite byte-code files created by another version of Python. The default
70 is to write files to their :pep:`3147` locations and names, which allows
71 byte-code files from multiple versions of Python to coexist.
Éric Araujo713d3032010-11-18 16:38:46 +000072
Éric Araujo930df312010-12-16 06:28:48 +000073.. versionchanged:: 3.2
74 Added the ``-i``, ``-b`` and ``-h`` options.
Éric Araujof68fa052010-12-16 02:10:11 +000075
Éric Araujo01606de2011-03-26 03:22:55 +010076There is no command-line option to control the optimization level used by the
77:func:`compile` function, because the Python interpreter itself already
78provides the option: :program:`python -O -m compileall`.
Éric Araujo713d3032010-11-18 16:38:46 +000079
80Public functions
81----------------
Georg Brandl116aa622007-08-15 14:28:22 +000082
Georg Brandl8334fd92010-12-04 10:26:46 +000083.. 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 +000084
85 Recursively descend the directory tree named by *dir*, compiling all :file:`.py`
R. David Murray94f58c32010-12-17 16:29:07 +000086 files along the way.
87
88 The *maxlevels* parameter is used to limit the depth of the recursion; it
89 defaults to ``10``.
90
91 If *ddir* is given, it is prepended to the path to each file being compiled
92 for use in compilation time tracebacks, and is also compiled in to the
93 byte-code file, where it will be used in tracebacks and other messages in
94 cases where the source file does not exist at the time the byte-code file is
95 executed.
96
Georg Brandl116aa622007-08-15 14:28:22 +000097 If *force* is true, modules are re-compiled even if the timestamps are up to
98 date.
99
R. David Murray94f58c32010-12-17 16:29:07 +0000100 If *rx* is given, its search method is called on the complete path to each
101 file considered for compilation, and if it returns a true value, the file
102 is skipped.
Georg Brandl116aa622007-08-15 14:28:22 +0000103
R. David Murray94f58c32010-12-17 16:29:07 +0000104 If *quiet* is true, nothing is printed to the standard output unless errors
105 occur.
Georg Brandl116aa622007-08-15 14:28:22 +0000106
R. David Murray94f58c32010-12-17 16:29:07 +0000107 If *legacy* is true, byte-code files are written to their legacy locations
108 and names, which may overwrite byte-code files created by another version of
109 Python. The default is to write files to their :pep:`3147` locations and
110 names, which allows byte-code files from multiple versions of Python to
111 coexist.
Georg Brandl116aa622007-08-15 14:28:22 +0000112
Georg Brandl8334fd92010-12-04 10:26:46 +0000113 *optimize* specifies the optimization level for the compiler. It is passed to
114 the built-in :func:`compile` function.
Barry Warsaw28a691b2010-04-17 00:19:56 +0000115
Georg Brandl8334fd92010-12-04 10:26:46 +0000116 .. versionchanged:: 3.2
Éric Araujo930df312010-12-16 06:28:48 +0000117 Added the *legacy* and *optimize* parameter.
118
119
120.. function:: compile_file(fullname, ddir=None, force=False, rx=None, quiet=False, legacy=False, optimize=-1)
121
R. David Murray94f58c32010-12-17 16:29:07 +0000122 Compile the file with path *fullname*.
Éric Araujo930df312010-12-16 06:28:48 +0000123
R. David Murray94f58c32010-12-17 16:29:07 +0000124 If *ddir* is given, it is prepended to the path to the file being compiled
125 for use in compilation time tracebacks, and is also compiled in to the
126 byte-code file, where it will be used in tracebacks and other messages in
127 cases where the source file does not exist at the time the byte-code file is
128 executed.
Éric Araujo930df312010-12-16 06:28:48 +0000129
R. David Murray8b24aac2011-02-11 22:37:16 +0000130 If *rx* is given, its search method is passed the full path name to the
R. David Murray94f58c32010-12-17 16:29:07 +0000131 file being compiled, and if it returns a true value, the file is not
132 compiled and ``True`` is returned.
Éric Araujo930df312010-12-16 06:28:48 +0000133
R. David Murray94f58c32010-12-17 16:29:07 +0000134 If *quiet* is true, nothing is printed to the standard output unless errors
135 occur.
136
137 If *legacy* is true, byte-code files are written to their legacy locations
138 and names, which may overwrite byte-code files created by another version of
139 Python. The default is to write files to their :pep:`3147` locations and
140 names, which allows byte-code files from multiple versions of Python to
141 coexist.
Éric Araujo930df312010-12-16 06:28:48 +0000142
143 *optimize* specifies the optimization level for the compiler. It is passed to
144 the built-in :func:`compile` function.
145
146 .. versionadded:: 3.2
Georg Brandl8334fd92010-12-04 10:26:46 +0000147
148
149.. function:: compile_path(skip_curdir=True, maxlevels=0, force=False, legacy=False, optimize=-1)
Georg Brandl116aa622007-08-15 14:28:22 +0000150
151 Byte-compile all the :file:`.py` files found along ``sys.path``. If
R. David Murray94f58c32010-12-17 16:29:07 +0000152 *skip_curdir* is true (the default), the current directory is not included
153 in the search. All other parameters are passed to the :func:`compile_dir`
154 function. Note that unlike the other compile functions, ``maxlevels``
155 defaults to ``0``.
Georg Brandl8334fd92010-12-04 10:26:46 +0000156
157 .. versionchanged:: 3.2
Éric Araujo930df312010-12-16 06:28:48 +0000158 Added the *legacy* and *optimize* parameter.
Georg Brandl8334fd92010-12-04 10:26:46 +0000159
Georg Brandl116aa622007-08-15 14:28:22 +0000160
161To force a recompile of all the :file:`.py` files in the :file:`Lib/`
162subdirectory and all its subdirectories::
163
164 import compileall
165
166 compileall.compile_dir('Lib/', force=True)
167
168 # Perform same compilation, excluding files in .svn directories.
169 import re
Georg Brandl1aca9532013-04-14 12:02:43 +0200170 compileall.compile_dir('Lib/', rx=re.compile(r'[/\\][.]svn'), force=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000171
172
173.. seealso::
174
175 Module :mod:`py_compile`
176 Byte-compile a single source file.