avatarLaxfed Paulacy

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2363

Abstract

"hljs-built_in">input</span>(<span class="hljs-string">"Enter the first number: "</span>)) y = <span class="hljs-built_in">int</span>(<span class="hljs-built_in">input</span>(<span class="hljs-string">"Enter the second number: "</span>)) result = x / y <span class="hljs-keyword">except</span> (ValueError, ZeroDivisionError) <span class="hljs-keyword">as</span> ex: <span class="hljs-keyword">import</span> bpython bpython.embed(<span class="hljs-built_in">locals</span>())</pre></div><p id="3e0e">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.</p><h2 id="4fbf">Using the bpdb Debugger</h2><p id="6d22">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 <code>bpdb</code> debugger that comes with bpython. It's nearly identical to Python's <code>pdb</code> debugger, but has an additional <code>bpython</code> or <code>b</code> command that starts bpython at the current stack frame.</p><p id="04a3">To utilize <code>bpdb</code>, modify your script as follows:</p><div id="48d8"><pre><span class="hljs-variable">import</span> <span class="hljs-variable">bpdb</span>

<span class="hljs-variable">def</span> <span class="hljs-function"><span class="hljs-title">divide_numbers</span>(): <span class="hljs-variable">x</span> = <span class="hljs-title">int</span>(<span class="hljs-title">input</span>(<span class="hljs-string">"Enter the first number: "</span>))</span> <span class="hljs-variable">y</span> = <span class="hljs-function"><span class="hljs-title">int</span>(<span class="hljs-title">input</span>(<span class="hljs-string">"Enter the second number: "</span>))</span> <span class="hljs-variable"><span class="hljs-class">result</span></span> = <span class="hljs-variable">x</span> / <span class="hljs-variable">y</span> <span class="hljs-variable">bpdb.set_trace</span>()

<span class="hljs-function"><span class="hljs-title">divide_numbers</span>()</span></pre></div><p id="e4c2">Or, if you’re using Python 3.7 or later, you can use the built-in <code>breakpoint()</code> convenience function. However, by default, it's hardwired to the classic <code>pdb</code> debugge

Options

r. Explicitly import <code>bpdb</code> to use it with <code>breakpoint()</code>:</p><div id="5fa2"><pre><span class="hljs-variable">import</span> <span class="hljs-variable">bpdb</span>

<span class="hljs-variable">def</span> <span class="hljs-function"><span class="hljs-title">divide_numbers</span>(): <span class="hljs-variable">x</span> = <span class="hljs-title">int</span>(<span class="hljs-title">input</span>(<span class="hljs-string">"Enter the first number: "</span>))</span> <span class="hljs-variable">y</span> = <span class="hljs-function"><span class="hljs-title">int</span>(<span class="hljs-title">input</span>(<span class="hljs-string">"Enter the second number: "</span>))</span> <span class="hljs-variable"><span class="hljs-class">result</span></span> = <span class="hljs-variable">x</span> / <span class="hljs-variable">y</span> <span class="hljs-function"><span class="hljs-title">breakpoint</span>()</span>

<span class="hljs-function"><span class="hljs-title">divide_numbers</span>()</span></pre></div><p id="6ed4">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.</p><h2 id="15fc">Conclusion</h2><p id="5cd5">In this tutorial, you’ve learned how to debug Python code using bpython’s enhanced REPL and the <code>bpdb</code> 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.</p><div id="e012" class="link-block"> <a href="https://readmedium.com/python-sharing-your-python-repl-session-2c86457507ca"> <div> <div> <h2>PYTHON — Sharing Your Python REPL Session</h2> <div><h3>Programs must be written for people to read, and only incidentally for machines to execute. — Harold Abelson</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*wYEG_fleZ5AAw1Wi.jpeg)"></div> </div> </div> </a> </div></article></body>

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.

Debug
Bpython
ChatGPT
Python
Recommended from ReadMedium