docs/guides/debugging.md
marimo supports pdb, the Python debugger. Set breakpoints in your code using
the built-in breakpoint() function. When the code execution reaches a
breakpoint, it will pause, and you can inspect variables, step through the
code, and evaluate expressions.
Here's a live example of how to use breakpoint() in a marimo notebook cell. Type
help in the debugger for a list of commands.
/// marimo-embed
@app.cell
def _():
# Compute triangle numbers
triangle = 0
triangle_count = 20
for i in range(1, triangle_count):
triangle += i # T_i = sum of 1..i
# Debug at the 10th iteration
# as a sanity check. Should be 55.
if i == 10:
breakpoint()
return
///
!!! warning "The debugger blocks execution"
When the debugger is active, you cannot execute cells. Remember to continue
or quit the debugger to avoid hanging the notebook!
??? note "Adding breakpoints in the stack trace" Click the little bug icon in the stack trace to add breakpoints.
<video muted controls playsinline width="100%" align="center" src="/_static/docs-pdb-breakpoint.webm" alt="Animation showing how to click the bug icon to add PDB breakpoints">
</video>
Clicking on the cell link will also take you to the cell where the error occurred.
If your code raises an exception, you can use postmortem debugging to inspect the state of the program at the point where the exception occurred. Click on the "Launch debugger" button as shown below:
<video muted controls playsinline width="100%" align="center" src="/_static/docs-postmortem-debugging.webm" alt="Video demonstrating postmortem debugging with the Launch debugger button"> </video>Since marimo notebooks are standard Python files, you can run them as scripts from the command line. The following command will run your marimo notebook and drop you into the pdb debugger if an exception occurs, or if you hit a breakpoint.
python -m pdb your_script.py
marimo provides built-in integration with AI assistants to help you debug your notebooks.
When interacting with the AI chat, you can reference the notebook "Errors" with
the @-symbol to bring in comprehensive error information from your notebook,
making it easier to get targeted debugging help.
Provide context beyond just the error. Include information about:
Leverage marimo's debugging tools alongside AI. Use marimo's dataflow tools to understand cell relationships, then share this information with AI assistants for more targeted advice.
Ask specific questions. Instead of "Why is this broken?", try:
!!! tip AI assistants are particularly helpful for explaining marimo-specific concepts like reactive execution, cell dependencies, and the differences between marimo notebooks and traditional Jupyter notebooks.
marimo supports debugging with IDEs, like VS Code, which natively support the
debugpy library. This allows you to set breakpoints, step through code, and
inspect variables directly from your IDE.
You can debug marimo notebooks in VS Code using the following launch.json.
This launch configuration will debug a marimo notebook executing it as a
script.
{
"version": "0.2.0",
"configurations": [
{
"type": "python",
"request": "launch",
"name": "marimo Debug: script mode",
"program": "${file}",
"debugOptions": [
"--ignore", "*/site-packages/marimo/*"
]
},
]
}
Edit mode debugging allows the marimo editor to trigger breakpoints set in an IDE like VS Code. Running in this mode will automatically start your notebook in watch mode. Note that the file state and editor must be consistent for break points to correctly work. If debugging is not acting as expected, force a notebook save and toggle the relevant breakpoints.
Use the following launch.json configuration to enable edit mode debugging:
{
"version": "0.2.0",
"configurations": [
{
"type": "debugpy",
"request": "launch",
"name": "marimo Debug: edit mode",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}",
"env": {
"MARIMO_SCRIPT_EDIT": "1"
},
"justMyCode": false
}
]
}
!!! note This will disable marimo's internal debugging features.
!!! danger This mode is blocking in VS Code, so you will need to interact with the debugger in your editor to regain control of the marimo notebook.