Python Glossary Русский

all Python 3until 3.13Removed from the standard libraryterm

cgitb

removed modules

Detailed exception reports for CGI scripts, as HTML or text. Removed in Python 3.13 — use traceback and logging.

Syntax

import cgitb  # до 3.13

cgitb.enable() replaced sys.excepthook: an unhandled exception became a page with the code around the error and variable values, and cgitb.text() produced the same as text. PEP 594 dropped it along with cgi. How to port: traceback.format_exc() gives the traceback as a string, logging.exception() inside except sends it to the log; a custom handler goes into sys.excepthook; variable values come from traceback.TracebackException(..., capture_locals=True). Web frameworks show their own debug page. The standard-cgitb package (and legacy-cgi) on PyPI restores the module.

Examples

# Python 3.13+
try:
    import cgitb
except ModuleNotFoundError as e:
    print(type(e).__name__)  # → ModuleNotFoundError
# Python 3.13+
import traceback
try:
    1 / 0
except ZeroDivisionError:
    report = traceback.format_exc()
print(report.splitlines()[-1])  # → ZeroDivisionError: division by zero
import cgitb, sys
try:
    1 / 0
except ZeroDivisionError:
    report = cgitb.text(sys.exc_info())
print('ZeroDivisionError' in report)  # → True

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy