On this page
compileall
Prev: colorsys | Up: stdlib_module | Next: compression
Rel: sys, sysconfig, builtins, warnings, dataclasses
Ref: docs.python.org — compileall
Tags: #public #python3 #stdlib
Import
import compileall
Overview
Batch bytecode compiler for entire directory trees (compile_dir(), compile_file(), compile_path()), also runnable as python3 -m compileall. Use compileall in Docker builds, packaging steps, or CI checks to pre-generate .pyc files (reducing cold-start time) and catch SyntaxError across every module at once.
Quick Start
import compileall
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
src = Path(tmp) / "hello.py"
src.write_text("x = 1 + 2\n", encoding="utf-8")
ok = compileall.compile_dir(tmp, quiet=1)
assert ok is True
assert list((Path(tmp) / "__pycache__").glob("hello.*.pyc"))
Implementation Walkthrough
1. Pre-Compiling in CI or Container Builds
Run compileall with workers=0 (auto-detect CPU count) and quiet=1 (print errors only) to validate syntax and bake optimized bytecode:
import compileall
def precompile_tree(root: str) -> bool:
return compileall.compile_dir(
root,
maxlevels=10,
force=False,
quiet=1,
optimize=1, # strips asserts and __debug__ blocks
workers=0,
)
Gotchas & Boundaries
- Bytecode is version-specific:
.pycfiles are tagged with the exact CPython minor version (e.g.,cpython-313.pyc) and ignored by other interpreter versions. - Does not execute code:
compileallonly parses and compiles syntax—it catchesSyntaxError, notImportErroror runtime bugs. - Reproducible builds (
invalidation_mode): Passinvalidation_mode=py_compile.PycInvalidationMode.CHECKED_HASHwhen building deterministic container layers where file mtimes vary.
Common Functions
- compileall.compile_dir() —
compile_dir(dir, maxlevels=None, ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1, workers=1, invalidation_mode=None, *, stripdir=None, prependdir=None, limit_sl_dest=None, hardlink_dupes=False)— Byte-compile all modules in the given directory tree. - compileall.compile_file() —
compile_file(fullname, ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1, invalidation_mode=None, *, stripdir=None, prependdir=None, limit_sl_dest=None, hardlink_dupes=False)— Byte-compile one file. - compileall.compile_path() —
compile_path(skip_curdir=1, maxlevels=0, force=False, quiet=0, legacy=False, optimize=-1, invalidation_mode=None)— Byte-compile all module on sys.path.
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/colorsys
- tuts/python3/stdlib_module/compileall/compile_dir
- tuts/python3/stdlib_module/compileall/compile_file
- tuts/python3/stdlib_module/compileall/compile_path
- tuts/python3/stdlib_module/compression
- tuts/python3/stdlib_module/py_compile
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/tutorials/intermediate_python_and_software_engineering/type_hinting_and_static_analysis
Related notes
- tuts/python3/stdlib_module/colorsys Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/compileall/compile_dir Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/compileall/compile_file Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/compileall/compile_path Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/compression Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/builtins Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/dataclasses Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/py_compile links to this page; shared tags: #stdlib