
PYTHON — Debug with bpython
Data is the new oil. It’s valuable, but if unrefined it cannot really be used. — Clive Humby
Debug With bpython
When working with Python, debugging is an essential skill to have. The bpython enhanced REPL (Read-Eval-Print Loop) offers a set of powerful tools for debugging and introspection. This tutorial will guide you through leveraging bpython’s features for effective debugging.
Embedding a bpython REPL
After installing bpython in a virtual environment, you can run the bpython command to start a new REPL session. Additionally, you can import bpython's internal modules into your regular scripts, which can be particularly useful for debugging purposes.
Let’s consider a scenario where you want to perform postmortem debugging after intercepting an exception. You can embed and start the bpython REPL right after your script crashes to introspect local variables using the dynamic nature of Python.
Here’s an example of a script that expects the user to enter two numbers and then divides one by the other. If the user provides a non-integer value for either of the two variables, Python will raise a ValueError. When both values entered by the user are valid integers, but the second one is equal to zero, a ZeroDivisionError will occur.
try:
x = int(input("Enter the first number: "))
y = int(input("Enter the second number: "))
result = x / y
except (ValueError, ZeroDivisionError) as ex:
import bpython
bpython.embed(locals())When an exception occurs, the script catches both exception types and embeds a bpython REPL with the local variables, allowing you to inspect and manipulate them to gain additional insight into the issue.
Using the bpdb Debugger
If you want to use a proper debugger to step through your code and manipulate the local variables at any point in the execution, you can use the bpdb debugger that comes with bpython. It's nearly identical to Python's pdb debugger, but has an additional bpython or b command that starts bpython at the current stack frame.
To utilize bpdb, modify your script as follows:
import bpdb
def divide_numbers():
x = int(input("Enter the first number: "))
y = int(input("Enter the second number: "))
result = x / y
bpdb.set_trace()
divide_numbers()Or, if you’re using Python 3.7 or later, you can use the built-in breakpoint() convenience function. However, by default, it's hardwired to the classic pdb debugger. Explicitly import bpdb to use it with breakpoint():
import bpdb
def divide_numbers():
x = int(input("Enter the first number: "))
y = int(input("Enter the second number: "))
result = x / y
breakpoint()
divide_numbers()Now, when your program reaches the breakpoint, it will pause its normal execution and drop you into the debugger, where you can step through the code line by line and interact with local variables.
Conclusion
In this tutorial, you’ve learned how to debug Python code using bpython’s enhanced REPL and the bpdb debugger. The ability to embed a bpython REPL and use a proper debugger provides valuable tools for identifying and resolving issues in your Python code. These techniques can significantly aid in improving your debugging workflow and overall development process.






