HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

8) Debugging Lesson

Python Debugger Options

7 min to complete · By Martin Breuss

Python: Debugger Options

The text-heavy black-and-white style of the pdb debugger can seem unintuitive to some people. If you prefer a more visual interface, you can swap out pdb for another debugger, for example:

  • pudb: a more visual command-line-based debugger
  • web-pdb: a visual debugger displayed in your web browser with clickable UI

In this lesson, you'll look at these two alternatives to pdb and learn how you can swap your debugger without needing to change your code.

Colorful CLI Debugger With Variable Inspector

The first alternative debugger, pudb, sounds very similar to pdb. However, if you use it, you'll see that it's a whole different visual experience.

pudb is a third-party package, which means that you need to install it with pip before you'll be able to use it:

python3 -m pip install pudb
Illustration of a lighthouse

Note: Remember to create and activate a virtual environment before installing any external packages on your local machine.

After installing pudb, you need to edit the relevant environment variable, PYTHONBREAKPOINT. This will instruct Python to use the newly installed debugger instead of the default one:

export PYTHONBREAKPOINT=pudb.set_trace

After setting the PYTHONBREAKPOINT environment variable to pudb.set_trace, you can execute your script in the same way you did before:

python3 your_file_name.py

Python will now launch pudb for you where it launched pdb before:

A pudb session in the CLI

As you can see, this looks quite different from the strictly text-based pdb that you encountered in the previous lessons. A notable improvement is the variable inspector in the top right of your terminal, where you can see all currently defined variables and their values at the line where pudb is currently pointing to.

You can use the same commands you learned before to step through your program and investigate what's going on in there.

Web-Based Debugger With Clickable User Interface

Another option you can install as an external package is web-pdb. This debugger provides an even more user-friendly visual interface. It'll start a server on your localhost that you can access in your browser. The UI of web-pdb even allows you to click buttons instead of typing the pdb commands.

Start by installing the package using pip inside of your virtual environment:

python3 -m pip install web-pdb

Just as you did with pudb before, you'll need to edit the PYTHONBREAKPOINT environment variable to point to web-pdb instead:

export PYTHONBREAKPOINT=web_pdb.set_trace

Now you can execute your script in the same way you did before:

python3 your_file_name.py

Your console will show you some output that indicates that the web server has been started:

2031-03-07 19:51:28,894: root - web_console:108 - CRITICAL - Web-PDB: starting web-server on MacBook-Air.local:5555...

When you see this output, you can open up your browser and go to the following URL:

http://localhost:5555/

Once the page has loaded, you should see the interface of a modern user-friendly debugger:

A web-pdb debugger session in a browser window

You'll notice that there's also a variable inspector at the top right of the interface, and it generally looks quite similar to pudb. However, you can use buttons to interact with your interactive debugger session.

Colorful illustration of a light bulb

Additional Resources

In a future lesson on debugging, you'll see how you can use a built-in debugger that comes with most modern IDEs.

Summary: Python, Debugger Options

  • By default, breakpoint() calls Python's standard library debugger pdb. This debugger comes with the standard library, but sometimes, you might want to use a more visual interface to make your debugging more straightforward.
  • You only need to change a single environment variable, PYTHONBREAKPOINT, to use a different debugger. You don't need to change anything in your code.
  • This can allow you to debug with different tools in different environments. For example, you could use a debugger with a graphical user interface on your local development machine and use a plain text one on your server without needing to change the code.

Change the Relevant Environment Variable:

export PYTHONBREAKPOINT=web_pdb.set_trace
  • The command shown above sets the environment variable PYTHONBREAKPOINT to use web_pdb as your debugger instead of the default pdb. If you now run your script, a different debugger session will be started.