How to step through Python code to help debug issues?

前端 未结 14 856
梦如初夏
梦如初夏 2020-11-22 11:17

In Java/C# you can easily step through code to trace what might be going wrong, and IDE\'s make this process very user friendly.

Can you trace through python code in

14条回答
  •  粉色の甜心
    2020-11-22 11:26

    ipdb (IPython debugger)

    ipdb adds IPython functionality to pdb, offering the following HUGE improvements:

    • tab completion
    • show more context lines
    • syntax highlight

    Much like pdg, ipdb is still far from perfect and completely rudimentary if compared to GDB, but it is already a huge improvement over pdb.

    Usage is analogous to pdb, just install it with:

    python3 -m pip install --user ipdb
    

    and then add to the line you want to step debug from:

    __import__('ipdb').set_trace(context=21)
    

    You likely want to add a shortcut for that from your editor, e.g. for Vim snipmate I have:

    snippet ipd
        __import__('ipdb').set_trace(context=21)
    

    so I can type just ipd and it expands to the breakpoint. Then removing it is easy with dd since everything is contained in a single line.

    context=21 increases the number of context lines as explained at: How can I make ipdb show more lines of context while debugging?

    Alternatively, you can also debug programs from the start with:

    ipdb3 main.py
    

    but you generally don't want to do that because:

    • you would have to go through all function and class definitions as Python reads those lines
    • I don't know how to set the context size there without hacking ipdb. Patch to allow it: https://github.com/gotcha/ipdb/pull/155

    Or alternatively, as in raw pdb 3.2+ you can set some breakpoints from the command line:

    ipdb3 -c 'b 12' -c 'b myfunc' ~/test/a.py
    

    although -c c is broken for some reason: https://github.com/gotcha/ipdb/issues/156

    python -m module debugging has been asked at: How to debug a Python module run with python -m from the command line? and since Python 3.7 can be done with:

    python -m pdb -m my_module
    

    Serious missing features of both pdb and ipdb compared to GDB:

    • persistent command history across sessions: Save command history in pdb

    ipdb specific annoyances:

    • multithreading does not work well if you don't hack some settings...
      • ipdb, multiple threads and autoreloading programs causing ProgrammingError
      • https://github.com/gotcha/ipdb/issues/51

    Tested in Ubuntu 16.04, ipdb==0.11, Python 3.5.2.

提交回复
热议问题