On this page
py_compile.compile
Up: py_compile | Next: py_compile.main
Rel: py_compile.main, py_compile.PycInvalidationMode, py_compile.PyCompileError
Ref: docs.python.org — py_compile.compile
Tags: #public #python3 #stdlib #py_compile
Import
import py_compile
from py_compile import compile
Signature & Overview
py_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.
Usage
from pathlib import Path
import py_compile, tempfile
with tempfile.TemporaryDirectory() as tmp:
src = Path(tmp) / "ok.py"
src.write_text("x = 1\n", encoding="utf-8")
pyc = py_compile.compile(str(src), doraise=True)
assert Path(pyc).exists()
Gotchas
- Always pass
doraise=Truewhen using as a syntax check.
REPL Inspect
from py_compile import compile
help(compile)
Linked from
Related notes
- tuts/python3/stdlib_module/py_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/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib
- tuts/c/stdlib/assert/static_assert shared tags: #stdlib
- tuts/c/stdlib/ctype shared tags: #stdlib
- tuts/c/stdlib/ctype/isalpha shared tags: #stdlib