blob: b0a2e34d911c60a0d0a35d4daff054603eadc156 [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
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
Benjamin Peterson5478b472008-09-17 22:25:09 +000013This module may also be used as a script (using the :option:`-m` Python flag) to
14compile Python sources. Directories to recursively traverse (passing
15:option:`-l` stops the recursive behavior) for sources are listed on the command
16line. If no arguments are given, the invocation is equivalent to ``-l
17sys.path``. Printing lists of the files compiled can be disabled with the
18:option:`-q` flag. In addition, the :option:`-x` option takes a regular
19expression argument. All files that match the expression will be skipped.
Barry Warsaw28a691b2010-04-17 00:19:56 +000020The :option:`-b` flag may be given to write legacy ``.pyc`` file path names,
21otherwise :pep:`3147` style byte-compiled path names are written.
Georg Brandl116aa622007-08-15 14:28:22 +000022
23
Barry Warsaw28a691b2010-04-17 00:19:56 +000024.. function:: compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None, quiet=False, legacy=False)
Georg Brandl116aa622007-08-15 14:28:22 +000025
26 Recursively descend the directory tree named by *dir*, compiling all :file:`.py`
27 files along the way. The *maxlevels* parameter is used to limit the depth of
28 the recursion; it defaults to ``10``. If *ddir* is given, it is used as the
29 base path from which the filenames used in error messages will be generated.
30 If *force* is true, modules are re-compiled even if the timestamps are up to
31 date.
32
33 If *rx* is given, it specifies a regular expression of file names to exclude
34 from the search; that expression is searched for in the full path.
35
36 If *quiet* is true, nothing is printed to the standard output in normal
37 operation.
38
Barry Warsaw28a691b2010-04-17 00:19:56 +000039 If *legacy* is true, old-style ``.pyc`` file path names are written,
40 otherwise (the default), :pep:`3147` style path names are written.
Georg Brandl116aa622007-08-15 14:28:22 +000041
Barry Warsaw28a691b2010-04-17 00:19:56 +000042
43.. function:: compile_path(skip_curdir=True, maxlevels=0, force=False, legacy=False)
Georg Brandl116aa622007-08-15 14:28:22 +000044
45 Byte-compile all the :file:`.py` files found along ``sys.path``. If
46 *skip_curdir* is true (the default), the current directory is not included in
Barry Warsaw28a691b2010-04-17 00:19:56 +000047 the search. The *maxlevels* parameter defaults to ``0``, and the *force*
48 and *legacy* parameters default to ``False``. All are
Georg Brandl116aa622007-08-15 14:28:22 +000049 passed to the :func:`compile_dir` function.
50
51To force a recompile of all the :file:`.py` files in the :file:`Lib/`
52subdirectory and all its subdirectories::
53
54 import compileall
55
56 compileall.compile_dir('Lib/', force=True)
57
58 # Perform same compilation, excluding files in .svn directories.
59 import re
60 compileall.compile_dir('Lib/', rx=re.compile('/[.]svn'), force=True)
61
62
63.. seealso::
64
65 Module :mod:`py_compile`
66 Byte-compile a single source file.
67