blob: d57669621a0cf2547955da69edb27fab7203f616 [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
9libraries. These functions compile Python source files in a directory tree,
10allowing users without permission to write to the libraries to take advantage of
11cached byte-code files.
12
Georg Brandl8ec7f652007-08-15 14:28:01 +000013
Éric Araujoa8132ec2010-12-16 03:53:53 +000014Command-line use
15----------------
16
17This module can work as a script (using :program:`python -m compileall`) to
18compile Python sources.
19
20.. program:: compileall
21
22.. cmdoption:: [directory|file]...
23
24 Positional arguments are files to compile or directories that contain
25 source files, traversed recursively. If no argument is given, behave as if
26 the command line was ``-l <directories from sys.path>``.
27
28.. cmdoption:: -l
29
30 Do not recurse.
31
32.. cmdoption:: -f
33
34 Force rebuild even if timestamps are up-to-date.
35
36.. cmdoption:: -q
37
38 Do not print the list of files compiled.
39
40.. cmdoption:: -d destdir
41
42 Purported directory name for error messages.
43
44.. cmdoption:: -x regex
45
46 Skip files with a full path that matches given regular expression.
47
48.. cmdoption:: -i list
49
50 Expand list with its content (file and directory names).
51
Éric Araujoc11ba762010-12-16 06:15:02 +000052.. versionchanged:: 2.7
53 Added the ``-i`` option.
Éric Araujoa8132ec2010-12-16 03:53:53 +000054
55
56Public functions
57----------------
Georg Brandl8ec7f652007-08-15 14:28:01 +000058
Éric Araujoc11ba762010-12-16 06:15:02 +000059.. function:: compile_dir(dir[, maxlevels[, ddir[, force[, rx[, quiet]]]]])
Georg Brandl8ec7f652007-08-15 14:28:01 +000060
61 Recursively descend the directory tree named by *dir*, compiling all :file:`.py`
62 files along the way. The *maxlevels* parameter is used to limit the depth of
63 the recursion; it defaults to ``10``. If *ddir* is given, it is used as the
64 base path from which the filenames used in error messages will be generated.
65 If *force* is true, modules are re-compiled even if the timestamps are up to
66 date.
67
68 If *rx* is given, it specifies a regular expression of file names to exclude
69 from the search; that expression is searched for in the full path.
70
71 If *quiet* is true, nothing is printed to the standard output in normal
72 operation.
73
Éric Araujoc11ba762010-12-16 06:15:02 +000074
75.. function:: compile_file(fullname[, ddir[, force[, rx[, quiet]]]])
76
77 Compile the file with path *fullname*. If *ddir* is given, it is used as the
78 base path from which the filename used in error messages will be generated.
79 If *force* is true, modules are re-compiled even if the timestamp is up to
80 date.
81
82 If *rx* is given, it specifies a regular expression which, if matched, will
83 prevent compilation; that expression is searched for in the full path.
84
85 If *quiet* is true, nothing is printed to the standard output in normal
86 operation.
87
88 .. versionadded:: 2.7
89
90
Georg Brandl8ec7f652007-08-15 14:28:01 +000091.. function:: compile_path([skip_curdir[, maxlevels[, force]]])
92
93 Byte-compile all the :file:`.py` files found along ``sys.path``. If
94 *skip_curdir* is true (the default), the current directory is not included in
95 the search. The *maxlevels* and *force* parameters default to ``0`` and are
96 passed to the :func:`compile_dir` function.
97
98To force a recompile of all the :file:`.py` files in the :file:`Lib/`
99subdirectory and all its subdirectories::
100
101 import compileall
102
103 compileall.compile_dir('Lib/', force=True)
104
105 # Perform same compilation, excluding files in .svn directories.
106 import re
107 compileall.compile_dir('Lib/', rx=re.compile('/[.]svn'), force=True)
108
109
110.. seealso::
111
112 Module :mod:`py_compile`
113 Byte-compile a single source file.