On this page
py_compile.PyCompileError
Prev: py_compile.PycInvalidationMode | Up: py_compile
Rel: py_compile.compile, py_compile.main, py_compile.PycInvalidationMode
Ref: docs.python.org โ py_compile.PyCompileError
Tags: #public #python3 #stdlib #py_compile
Import
import py_compile
from py_compile import PyCompileError
Signature & Overview
py_compile.PyCompileError(exc_type, exc_value, file, msg='')
Exception raised when an error occurs while attempting to compile the file.
Methods & Attributes
PyCompileError.add_note(self, note, /)โ Add a note to the exceptionPyCompileError.with_traceback(self, tb, /)โ Set self.traceback to tb and return self.
Usage
from pathlib import Path
import py_compile, tempfile
with tempfile.TemporaryDirectory() as tmp:
bad = Path(tmp) / "bad.py"
bad.write_text("def broken(:\n", encoding="utf-8")
try:
py_compile.compile(str(bad), doraise=True)
except py_compile.PyCompileError as exc:
assert "bad.py" in exc.msg
Gotchas
- Only raised when
doraise=True: Otherwisepy_compile.compile()prints to stderr and returnsNone.
REPL Inspect
from py_compile import PyCompileError
help(PyCompileError)
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/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/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