python-debugger
Debug Python errors, exceptions, and unexpected behavior. Analyzes tracebacks, reproduces issues, identifies root causes, and provides fixes.
What this skill does
# Python Debugger
## Debugging Process
1. Understand the Error -> 2. Reproduce -> 3. Isolate -> 4. Identify Root Cause -> 5. Fix -> 6. Verify
## Step 1: Understand the Error
### Reading Tracebacks
```
Traceback (most recent call last): <- Read bottom to top
File "app.py", line 45, in main <- Entry point
result = process_data(data) <- Call chain
File "processor.py", line 23, in process_data
return transform(item) <- Getting closer
File "transformer.py", line 12, in transform
return item["value"] / item["count"] <- Error location
ZeroDivisionError: division by zero <- The actual error
```
Common error types: see [references/python-error-types.md](references/python-error-types.md)
## Step 2: Reproduce the Issue
Create a minimal test case that triggers the error. Answer these questions:
- What input triggered this?
- Is it consistent or intermittent?
- When did it start happening?
- What changed recently?
## Step 3: Isolate the Problem
### Print Debugging
```python
def process_data(data):
print(f"DEBUG: data type = {type(data)}")
print(f"DEBUG: data = {data}")
for i, item in enumerate(data):
print(f"DEBUG: processing item {i}: {item}")
result = transform(item)
print(f"DEBUG: result = {result}")
return results
```
### Using pdb
```python
import pdb
def problematic_function(x):
pdb.set_trace() # Execution stops here
# Or use: breakpoint() # Python 3.7+
result = x * 2
return result
```
pdb commands: see [references/pdb-commands.md](references/pdb-commands.md)
### Using icecream
```python
from icecream import ic
def calculate(x, y):
ic(x, y) # Prints: ic| x: 5, y: 0
result = x / y
ic(result)
return result
```
## Step 4: Common Root Causes
- **None values**: Check return values before accessing attributes. Guard with `if x is None: raise ValueError(...)`
- **Type mismatches**: Add type hints, cast inputs explicitly. `int(a) + int(b)` not `a + b`
- **Mutable default arguments**: Use `def f(items=None):` then `items = items or []` inside
- **Circular imports**: Use lazy imports inside functions: `from .module import Class`
- **Async/await**: Missing `await` returns coroutine instead of result
- **Key/Index errors**: Use `.get(key, default)` for dicts, check `len()` for lists
- **Scope issues**: `global`/`nonlocal` declarations, closure variable capture in loops
- **Encoding**: Specify `encoding="utf-8"` in `open()` calls
- **Float precision**: Use `decimal.Decimal` or `math.isclose()` for comparisons
- **Resource leaks**: Use `with` statements for files, connections, locks
## Step 5: Fix Patterns
### Defensive Programming
```python
def safe_divide(a, b):
if b == 0:
raise ValueError("Cannot divide by zero")
return a / b
def safe_get(data: dict, key: str, default=None):
return data.get(key, default)
```
### Input Validation
```python
def process_user(user_id: int, data: dict) -> dict:
if not isinstance(user_id, int) or user_id <= 0:
raise ValueError(f"Invalid user_id: {user_id}")
required_fields = ["name", "email"]
missing = [f for f in required_fields if f not in data]
if missing:
raise ValueError(f"Missing required fields: {missing}")
```
### Exception Handling
```python
import logging
logger = logging.getLogger(__name__)
def fetch_user_data(user_id: int) -> dict:
try:
response = api_client.get(f"/users/{user_id}")
response.raise_for_status()
return response.json()
except requests.HTTPError as e:
logger.error(f"HTTP error fetching user {user_id}: {e}")
raise
except requests.ConnectionError:
logger.error(f"Connection failed for user {user_id}")
raise ServiceUnavailableError("API unavailable")
```
## Step 6: Verify the Fix
```python
import pytest
def test_transform_handles_zero_count():
"""Verify fix for ZeroDivisionError."""
data = {"value": 10, "count": 0}
with pytest.raises(ValueError, match="count cannot be zero"):
transform(data)
def test_transform_normal_case():
"""Verify normal operation still works."""
data = {"value": 10, "count": 2}
result = transform(data)
assert result == 5
```
## Debugging Tools
### Logging Setup
```python
import logging
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s %(name)s %(levelname)s: %(message)s",
handlers=[
logging.FileHandler("debug.log"),
logging.StreamHandler(),
],
)
```
### Profiling
```python
# Time profiling
import cProfile
cProfile.run("main()", "output.prof")
# Memory profiling
from memory_profiler import profile
@profile
def memory_heavy_function():
# ...
```
### Using rich for better output
```python
from rich.traceback import install
install(show_locals=True) # Enhanced tracebacks
```
## References
- [Debug Checklist](references/debug-checklist.md)
- [Common Error Types](references/python-error-types.md)
- [pdb Commands](references/pdb-commands.md)
## When to Use WebSearch
- Cryptic error messages
- Library-specific errors
- Version compatibility issues
- Undocumented behavior
Related in Code Review
gstack
IncludedFast headless browser for QA testing and site dogfooding. Navigate pages, interact with elements, verify state, diff before/after, take annotated screenshots, test responsive layouts, forms, uploads, dialogs, and capture bug evidence. Use when asked to open or test a site, verify a deployment, dogfood a user flow, or file a bug with screenshots. (gstack)
startup-due-diligence
IncludedLegal due diligence review for seed-stage and Series A startups (US, Delaware C-Corp focus). Supports both investor and founder perspectives. Capabilities include: (1) Interactive document review and issue spotting; (2) Document request list generation; (3) Cap table and SAFE/convertible note analysis; (4) Red flag identification with severity ratings; (5) Diligence report generation. TRIGGERS: due diligence, DD, startup investment, cap table review, Series A, seed round, investor diligence, legal review startup, SAFE analysis, convertible note, 409A, founder vesting.
interview-master
IncludedThis skill should be used when the user asks to "generate interview questions", "prepare for interview", "optimize resume", "conduct mock interview", "analyze git commits for resume", "generate resume from code", "review my resume", or mentions interview preparation, career assistance, or extracting project experience from git history. Provides comprehensive interview and career development guidance for both job seekers and interviewers.
fix-issue
IncludedFixes GitHub issues using parallel analysis agents for root cause investigation, code exploration, and regression detection. Reads issue context from gh CLI, searches codebase and memory for related patterns, generates a fix with tests, and links the resolution back to the issue via PR. Includes prevention analysis to avoid recurrence. Use when debugging errors, resolving regressions, fixing bugs, or triaging issues.
sf-apex
IncludedGenerates and reviews Salesforce Apex code with 150-point scoring. TRIGGER when: user writes, reviews, or fixes Apex classes, triggers, test classes, batch/queueable/schedulable jobs, or touches .cls/.trigger files. DO NOT TRIGGER when: LWC JavaScript (use sf-lwc), Flow XML (use sf-flow), SOQL-only queries (use sf-soql), or non-Salesforce code.
swift-development
IncludedComprehensive Swift development for building, testing, and deploying iOS/macOS applications. Use when Claude needs to: (1) Build Swift packages or Xcode projects from command line, (2) Run tests with XCTest or Swift Testing framework, (3) Manage iOS simulators with simctl, (4) Handle code signing, provisioning profiles, and app distribution, (5) Format or lint Swift code with SwiftFormat/SwiftLint, (6) Work with Swift Package Manager (SPM), (7) Implement Swift 6 concurrency patterns (async/await, actors, Sendable), (8) Create SwiftUI views with MVVM architecture, (9) Set up Core Data or SwiftData persistence, or any other Swift/iOS/macOS development tasks.