Debugging Python in Spyder: Variable Explorer and Breakpoints
Learn to use Spyder's Variable Explorer for debugging Python scientific code. Covers conditional breakpoints, post-mortem debugging, and verification steps.
02 Jul 2026, 12:18 UTC

Problem: Debugging Scientific Python Code with Variable Explorer
When working with numpy arrays, pandas DataFrames, or custom objects in Spyder, you need to inspect live variables during debugging sessions. The Variable Explorer provides this capability, but large objects can freeze the UI and complex debugging workflows require specific techniques.
Prerequisites
- Spyder 5.0 or later installed
- Python 3.7+ with numpy and pandas
- A Python script with debuggable code
- Basic familiarity with breakpoints
Setting Up Your Debugging Session
Create a test file with the following code:
import numpy as np
import pandas as pd
def process_data():
data = np.random.rand(1000, 1000) # Large array
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
result = df['A'] * 2
return result
# Set breakpoint on next line
output = process_data()
print(output)
Run the debugger by clicking the bug icon or pressing Ctrl+F5. The Variable Explorer should populate with local variables including data, df, and result.
Using Variable Explorer Effectively
Show in Editor: Right-click any variable and select 'Show in Editor' to create an interactive debugging panel. This opens a new pane with the variable's contents, allowing you to explore nested structures without expanding in the main explorer.
Filtering Variables: Click the funnel icon in Variable Explorer to filter out internal IPython objects. Select 'User defined' to see only your script's variables, reducing clutter during debugging.
Debug Console: The debug console provides a dedicated IPython session connected to the current frame's namespace. Type expressions like df.head() or data.mean() to evaluate them with access to local variables.
Conditional Breakpoints
Right-click a line number to set a conditional breakpoint. For example, add this loop to your script:
for i in range(10):
if i == 5:
result = i * 10
print(i)
Right-click the line result = i * 10 and enter the condition i == 5. The debugger will only break when this expression evaluates to True.
Post-Mortem Debugging
When your script crashes, Spyder can load the error state into the debugger. Add this to trigger an error:
df = pd.DataFrame({'A': [1, 2, 3]})
result = df['B'].sum() # KeyError: 'B' does not exist
After the error, click the 'Go to last exception' button to enter post-mortem debugging. The Variable Explorer will show df and the current exception state.
Expected Checks
- Variable Population: After running the debugger, verify that data, df, and result appear in Variable Explorer with their correct types.
- Conditional Breakpoint Test: Run the loop code and confirm the debugger stops only when i equals 5, not on every iteration.
- Debug Console Evaluation: In the debug console, type result and verify it returns the expected output from your function.
- Post-Mortem State: After triggering the KeyError, confirm the Variable Explorer shows df and the exception information.
Performance Considerations
Large Arrays: Expanding large numpy arrays in Variable Explorer can freeze the UI. Use the debug console to evaluate data.shape or data.mean() instead of expanding the full array.
Custom __repr__: Objects with custom string representation methods may slow down variable loading. If debugging becomes sluggish, consider temporarily disabling custom __repr__ methods or using Show in Editor instead of expanding in the explorer.
Multi-Threading Limitations
Debugging multi-threaded Python applications may show inconsistent variable states. The debugger primarily tracks the main thread's execution context. For thread-specific debugging, use Python's built-in logging or the threading module's debug facilities.
Remote Debugging
For remote debugging sessions, you need to manually configure the debug server port and ensure firewall settings allow the connection. This requires additional setup beyond the standard debugging workflow described above.
Recovery Options
If the debugger becomes unresponsive:
- Press Ctrl+C to interrupt execution
- Close the debug console and restart the debugging session
- Use the Stop button to terminate the debugging session cleanly
Verification Steps
- Create a new Python file with the sample code provided
- Set a breakpoint and run the debugger
- Confirm Variable Explorer populates with local variables
- Test conditional breakpoint functionality
- Trigger an error and verify post-mortem debugging works
These steps confirm that Spyder's debugging features are functioning correctly for your scientific computing workflows.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.