Language and Compiler Modules

compileall

Precompiles Python source files into bytecode cache files across a directory or for selected files. Compilation can detect syntax errors without running module bodies, but cached bytecode depends on the interpreter and invalidation policy. Use a disposable directory when experimenting.

Formal Syntax & Structure

python syntaxpython
import compileall

Return Value

module

Module namespace available through import; import is a statement, not a value-returning expression.

Working Example

Example Snippetpython
from tempfile import TemporaryDirectoryfrom pathlib import Pathimport compileallwith TemporaryDirectory() as folder:    source = Path(folder) / "demo.py"    source.write_text("value = 7\n", encoding="utf-8")    print(compileall.compile_dir(folder, quiet=2))
Python Reference