Register a sys.monitoring callback for execution events
Answer: sys.monitoring is a namespace within the sys module, not an independent module. import sys.monitoring would fail with a ModuleNotFoundError. Import sys and then use sys.monitoring. PEP 669 proposes this API for monitoring Python programs running on CPython at low cost.
The three parts
The monitoring API consists of three components: tool identifiers, events, and callbacks. The namespace exposes the functions and constants that activate and control event monitoring.
Tool identifiers
A tool identifier is an integer and the associated name. Identifiers are integers in the range 0 to 5 inclusive. Before registering or activating events, a tool should choose an identifier and call use_tool_id. Tool identifiers are used to discourage tools from interfering with each other. Multiple tools can operate at the same time.
| sys.monitoring | events | |---|---| | sys.monitoring | Namespace within the sys module | | tool identifiers | Integers in the range 0 to 5 inclusive | | events | Execution events of interest | | callbacks | Called when those events occur |
See also
Claims
- id: C1 claim: PEP 669 proposes a low-cost API for monitoring programs running on CPython. source: S1 evidence: "an API for monitoring Python programs running on CPython"
- id: C2 claim: A sys.monitoring namespace holds the functions and constants. source: S1 evidence: "A sys.monitoring namespace will be added"
- id: C3 claim: sys.monitoring is a namespace in sys, not an importable module. source: S2 evidence: "sys.monitoring is a namespace within the sys module"
Sources
- [S1] PEP 669 – Low Impact Monitoring for CPython. Python PEPs. https://peps.python.org/pep-0669/ . License: Public-Domain. Accessed 2026-11-19.
- [S2] sys.monitoring — Execution event monitoring. Python docs. https://docs.python.org/3/library/sys.monitoring.html . License: PSF-2.0. Accessed 2026-11-19.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python PEPs, PEP 669 – Low Impact Monitoring for CPython; Python docs, sys.monitoring — Execution event monitoring. Public-Domain; PSF-2.0. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python PEPs, PEP 669 – Low Impact Monitoring for CPython; Python docs, sys.monitoring — Execution event monitoring. Public-Domain; PSF-2.0.
Cite this
iinventedlight. 2026. "Register a sys.monitoring callback for execution events." iinventedlight Library, iil:note:ay5t6edz, version of 2026-11-19. https://iinventedlight.com/library/notes/python-sys-monitoring/. License: CC-BY-4.0.
@misc{iil_note_ay5t6edz,
author = {{iinventedlight}},
title = {{Register a sys.monitoring callback for execution events}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-19},
url = {https://iinventedlight.com/library/notes/python-sys-monitoring/},
note = {Permanent ID iil:note:ay5t6edz. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-sys-monitoring.table.json (table; CC-BY-4.0)
- python-sys-monitoring.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-sys-monitoring.md.