Python Compileall Module
The compileall module provides some utility functions to support installing Python libraries. It compiles Python source files in a directory tree. This module can be used to force compilation of all .py files in a directory.
Importing the Module
import compileall
Programmatic Usage
Compiling a Directory
To compile all .py files in a directory (and recursively in subdirectories), use compileall.compile_dir().
import compileall
# Compile all files in the current directory
compileall.compile_dir('.', force=True)
dir: The directory to compile.maxlevels: Depth of recursion (default 10).force: If True, force recompilation even if timestamps are up-to-date.quiet: If non-zero, suppress output.
Compiling a Single File
To compile a single file, use compileall.compile_file().
import compileall
compileall.compile_file('myscript.py')
Command Line Usage
The module can be executed as a script to compile Python sources.
python -m compileall .
Options
-l: Don't recurse into subdirectories.-f: Force rebuild even if timestamps are up-to-date.-q: Quiet operation.
# Compile current directory, force rebuild, quiet output
python -m compileall -f -q .