Debug With bpython
00:00
Debug with bpython. Once you install bpython in a virtual environment, you can run the bpython
command to start a new REPL session as usual.
00:10 However, you can also import any of bpython’s internal modules into your regular scripts. Some of them may become particularly useful for debugging. Let’s say you wanted to perform postmortem debugging after intercepting an exception. In such a case, you can embed and start the bpython REPL right after your script after it crashes to introspect local variables using the dynamic nature of Python.
00:37 The following script expects the user to enter two numbers, which are then divided one by the other.
00:54
If the user provides a non-integer value for either of the two variables, then Python will raise a ValueError
. When both values entered by the user are valid integers, but the second one is equal to zero, then you’ll end up with a ZeroDivisionError
instead.
01:08 The script catches both exception types and embeds a bpython REPL with the local variables in response. On-screen, you can see a sample execution of the code.
01:22
Remember that you must run the script from a virtual environment with bpython installed. Otherwise, you won’t be able to import its modules. As soon as there’s an exception, you are dropped into an interactive bpython session with access to all your local variables including ex
, x
, and y
, which you can inspect and manipulate to gain additional insight into the issue.
01:46
Embedding a bpython REPL may not be enough. If you want to use a proper debugger to step through your code and manipulate the local variables at any point in the execution, then you can use the bpdb
debugger that comes with bpython.
02:00
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 take advantage of bpdb
, you can modify your existing adder.py
script as seen on-screen.
02:22
The call to bpdb.set_trace()
creates a breakpoint that will interrupt the execution of the Python program and start the interactive debugger.
02:31
Note that since Python 3.7, you can tell the built-in breakpoint()
convenience function to have the same effect. Unfortunately, the function is hardwired to the classic pdb
debugger by default, so if you want to take advantage of bpdb
instead, then you should import it explicitly as in the example seen on-screen.
02:54
You can control which command breakpoint()
delegates by setting the PYTHONBREAKPOINT
environment variable. For example, if you set its value to bpdb.set_trace
, then breakpoint()
will always start the bpdb
debugger.
03:09 Now, when your program reaches that breakpoint, it will pause its normal execution and drop you into the debugger. You
03:20
can step through the code line by line. typing the lowercase letter n
and confirming with Enter advances to the next line. At any point, you can type the uppercase letter B
to embed the bpython REPL.
03:45 You can exit the debugger in the same way you typically exit the REPL—i.e., hit and Ctrl + D to send the end-of-file character. Naturally, bpython comes with many more useful modules that you can take advantage of for purposes other than debugging. For instance, you could leverage its excellent code introspection mechanism if you’re writing a static code analysis tool or something similar.
04:11 In the next section of the course, you’ll take a look at the steps needed to contribute to bpython.
Become a Member to join the conversation.