This module provides some utility functions to support installing Python libraries. These functions compile Python source files in a directory tree, allowing users without permission to write to the libraries to take advantage of cached byte-code files.
This module can work as a script (using python -m compileall) to compile Python sources.
Recursively descend the directory tree named by dir, compiling all .py files along the way. The maxlevels parameter is used to limit the depth of the recursion; it defaults to 10. If ddir is given, it is used as the base path from which the filenames used in error messages will be generated. If force is true, modules are re-compiled even if the timestamps are up to date.
If rx is given, it specifies a regular expression of file names to exclude from the search; that expression is searched for in the full path.
If quiet is true, nothing is printed to the standard output in normal operation.
To force a recompile of all the .py files in the Lib/ subdirectory and all its subdirectories:
import compileall
compileall.compile_dir('Lib/', force=True)
# Perform same compilation, excluding files in .svn directories.
import re
compileall.compile_dir('Lib/', rx=re.compile('/[.]svn'), force=True)
See also