How to debug python + curses? [was: RE: Applying winpdb_reborn]

Chris Angelico rosuav at gmail.com
Sun May 30 17:46:52 EDT 2021


On Mon, May 31, 2021 at 7:03 AM Alan Gauld via Python-list
<python-list at python.org> wrote:
>
> On 30/05/2021 18:26, pjfarley3 at earthlink.net wrote:
> > I tried winpdb-reborn some time last year on my Win10 system (python 3.8.3
> > at that time), but could not figure out how to use it to debug a python
> > script that uses the curses module.
>
> You are not alone. debugging curses is one of the biggest obstacles to
> its use.
>
> My approach is to define a status line at the bottom of my program and
> write print statements into that window. Something like:
>
> def main(stdwin):
>     appwin = curses.newwin(...) # LINES-1 high
>     status = curses.newwin(...) # 1 line high positioned on bottom
>     # more code here
>     status.addstr(0,0, "Value of foo = %s" % foo)
>
> curses.wrapper(main)
>
> After debugging the status window can either be retained as an
> application status bar or removed and the main window
> enlarged by one line...
>
> If anyone else has found a better way to debug curses code I'm
> also keen to hear!
>

Never had this problem with curses per se (partly because I've used it
very little), but a more general technique for debugging things that
don't have a "normal" console is to create one via a pipe or file. The
easiest way is something like:

log = open("logfile.txt", "w")
print(f"At this point, {foo=}", file=log, flush=True)

Then, in a separate window - or even on a completely different
machine, via SSH or equivalent - "tail -F logfile.txt" will be your
console.

ChrisA


More information about the Python-list mailing list