20

I'm using PDB a lot and it seems it would be even better if I could add systax highlighting in color.

Ideally, I'd like to have to the path to the code a lighter color. The line of actual code would be syntax highlighted.

I'm using OS X and the Terminal app. Python 2.7

5 Answers 5

26

pdb doesn't support colorization. However, it's not that hard to get it, even if you're a command-line addict (as I am;-) -- you don't have to switch to GUIs/IDEs just to get colorization while debugging Python. In particular, command-line tools usually work much better when you're accessing a remote machine via SSH, saving a lot of the bandwidth and latency issues that any remote access to GUIs and IDEs can inflict on you;-).

Specifically, for the task you're asking about, consider ipdb (you also need ipython, which offers a far more advanced shell than plain interactive Python, on which ipdb relies). Both offer you good tab completion, enhanced tracebacks, and colorization -- ipython for your normal interactive work, ipdb with the same features when you're debugging (otherwise just about the same as pdb).

Sign up to request clarification or add additional context in comments.

1 Comment

So do $ python -m ipdb <mymodule> and the output will be syntax highlighted. Using Python3 since this is ten years later.
11

Take a look at pdb++ - it is a drop-in replacement for pdb that fills all your requirements and adds some other nice features such as tab completion and new commands such as watch and sticky.

Here is a sample config file that will enable colours (add this after creating the file: touch ~/.pdbrc.py):

import pdb

class Config(pdb.DefaultConfig):
    use_pygments = True
    pygments_formatter_class = "pygments.formatters.TerminalTrueColorFormatter"
    pygments_formatter_kwargs = {"style": "monokai"}

Comments

9

You could try pudb, which works in the terminal and looks like this:

pudb screenshot

I haven't tried some of the options mentioned in other answers, but to judge from the PyPI pages, pudb is better maintained and better documented.

1 Comment

The current versions have a few color schemes that are quite nice (not gaudy like the default shown above)
0

This might not possible for you, but have you tried using a graphical debugger (like the one in eclipse/pydev)? It will give you your syntax highlighting and much more.

I only use pdb directly if I don't have an option, because a graphical interface is just that much nicer.

Comments

0

In case someone hit the problem with colorization in a console.

My console had white background while ipdb was also adding rather light colors to syntax (for example variables were white). Pressing man ipython shows that we have 3 colors available: 'nocolor', 'linux', 'LightBG'. Ipdb was in my case installed via easy_install into my virtualenv. So it was trivial to look into ipdb source and modify it (hint search for ipdb/init.py in your env). Then I've modified following:

def set_trace():
    ip = ipapi.get()
    + def_colors = ip.options.colors
    + def_colors = 'LightBG'
    Pdb(def_colors).set_trace(sys._getframe().f_back)

It's a kinda hackish solution but well its for debugging purpose on my working station so its sufficient. But if anyone finds something better. Please send me a message on what to do.

2 Comments

Does this work? i have IndentationError: unexpected indent on the lines with a + sign
That code was pseudo-code. pluses were only indications of where I put new lines of code (similar to how patches are formatted). Drop plusses and should work, on the other hand, it's 2020 so I stopped using it altogether, nowadays it works out of the box. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.