On this page
py_compile
Prev: pwd | Up: stdlib_module | Next: pyclbr
Rel: sys, sysconfig, builtins, warnings, dataclasses
Ref: docs.python.org — py_compile
Tags: #public #python3 #stdlib
Import
import py_compile
Overview
Single-file Python source-to-bytecode compiler (py_compile.compile()), underlying compileall. Use py_compile when you want programmatic control over compiling one .py file into a .pyc artifact, choosing an output path, or validating syntax with doraise=True.
Quick Start
from pathlib import Path
import py_compile
import tempfile
with tempfile.TemporaryDirectory() as tmp:
src = Path(tmp) / "mod.py"
src.write_text("def add(a, b): return a + b\n", encoding="utf-8")
pyc_path = py_compile.compile(str(src), doraise=True)
assert Path(pyc_path).exists()
Implementation Walkthrough
1. Checking Syntax Programmatically
Pass doraise=True so syntax errors raise py_compile.PyCompileError instead of merely printing to sys.stderr:
from pathlib import Path
import py_compile
import tempfile
with tempfile.TemporaryDirectory() as tmp:
bad = Path(tmp) / "broken.py"
bad.write_text("def broken(:\n pass\n", encoding="utf-8")
try:
py_compile.compile(str(bad), doraise=True)
assert False, "expected PyCompileError"
except py_compile.PyCompileError as exc:
assert "broken.py" in exc.msg
Gotchas & Boundaries
doraise=Falseis the default: Withoutdoraise=True,py_compile.compile()swallows syntax errors, prints them to stderr, and returnsNone.- Use
compileallfor trees: Reach for compileall when compiling whole packages or running in parallel. - No obfuscation:
.pycfiles can be disassembled trivially with dis and are not a security or IP-protection boundary.
Common Functions
- py_compile.compile() —
compile(file, cfile=None, dfile=None, doraise=False, optimize=-1, invalidation_mode=None, quiet=0)— Byte-compile one Python source file to Python bytecode. - py_compile.main() —
main()—py_compile.main()function.
Classes & Objects
- py_compile.PycInvalidationMode —
py_compile.PycInvalidationModeclass. - py_compile.PyCompileError — Exception raised when an error occurs while attempting to compile the file. (Methods:
.add_note(),.with_traceback())
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/pwd
- tuts/python3/stdlib_module/py_compile/compile
- tuts/python3/stdlib_module/py_compile/main
- tuts/python3/stdlib_module/py_compile/PycInvalidationMode
- tuts/python3/stdlib_module/py_compile/PyCompileError
- tuts/python3/stdlib_module/pyclbr
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/pwd Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/py_compile/compile Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/py_compile/main Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/py_compile/PycInvalidationMode Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/py_compile/PyCompileError Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pyclbr 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/compileall Linked here; shared tags: #stdlib