PDB Commands Every Odoo Dev Should Know
Debugging is an inevitable part of every developer’s life. If you’ve ever found yourself staring at an Odoo module that just won’t behave, you know the pain of tracing through logs, adding print() statements everywhere, and still not finding the issue.
Enter PDB (Python Debugger)—a lifesaver for Odoo developers. With a few simple commands, you can step through your code, inspect variables, and pinpoint bugs in minutes instead of hours.
In this guide, we’ll break down the must-know PDB commands that will make you a debugging pro. Whether you're a beginner or an experienced Odoo dev, mastering these will save you countless headaches.
🔧 Setting Up PDB in Odoo
Before diving into commands, let’s quickly cover how to set up PDB in your Odoo environment:
Insert the PDB breakpoint in your Python code:
import pdb; pdb.set_trace() # Execution will pause hereRun your Odoo server in terminal mode (not as a service) to see the debugger in action.
Once the breakpoint hits, you’ll see the (Pdb) prompt—this is where the magic happens.
🚀 Essential PDB Commands for Faster Debugging
1. n (Next Line)
- What it does: Executes the current line and moves to the next line in the same function.
- When to use: When you want to step through code line by line without diving into function calls.
Example:
def calculate_tax(amount):
pdb.set_trace()
tax = amount * 0.15 # Stops here
return tax # Press 'n' to move here
2. s (Step Into)
- What it does: Steps into a function call (unlike
n, which stays at the current level). - When to use: When you need to debug inside a nested function or method.
Example:
def apply_discount(total):
discount = calculate_discount(total) # Press 's' to enter calculate_discount()
return total - discount
3. c (Continue Execution)
- What it does: Resumes execution until the next breakpoint or program end.
- When to use: When you’ve checked the issue and want to let the code run normally.
4. p <variable> (Print Variable)
- What it does: Prints the current value of a variable.
- When to use: To inspect variables without stopping execution.
Example:
user = {"name": "John", "age": 30}
pdb.set_trace()
print(user) # Instead of this, type `p user` in PDB
5. pp <variable> (Pretty Print)
- What it does: Formats complex objects (like dictionaries, lists) in a readable way.
- When to use: When
pgives you a messy output (e.g., large JSON responses).
Example:
data = {"users": [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]}
pp data # Nicely formatted output
6. l (List Code Around Breakpoint)
- What it does: Shows the surrounding code (current line + a few lines above/below).
- When to use: When you need context on where you are in the execution.
7. q (Quit Debugger)
- What it does: Exits PDB immediately, stopping program execution.
- When to use: When you’ve found the bug and want to stop debugging.
💡 Pro Tips for Efficient Debugging
✅ Combine n and s to navigate functions efficiently.
✅ Use pp for API responses—Odoo recordsets and dictionaries become much clearer.
✅ Set conditional breakpoints by modifying pdb.set_trace() with logic:
if condition:
pdb.set_trace()
✅ Log variable history by printing them at different stages.
🎯 Final Thoughts
PDB is one of the fastest ways to debug Odoo modules—no need for heavy logging or guesswork. Once you master these commands, you’ll fix issues in minutes instead of hours.
Your turn: What’s your favorite PDB trick? Have you used any advanced techniques like post-mortem debugging (pdb.pm())? Share your tips below! 👇
Happy debugging! 🚀