How to step through Python code to help debug issues?

前端 未结 14 872
梦如初夏
梦如初夏 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

    Yes! There's a Python debugger called pdb just for doing that!

    You can launch a Python program through pdb by using pdb myscript.py or python -m pdb myscript.py.

    There are a few commands you can then issue, which are documented on the pdb page.

    Some useful ones to remember are:

    • b: set a breakpoint
    • c: continue debugging until you hit a breakpoint
    • s: step through the code
    • n: to go to next line of code
    • l: list source code for the current file (default: 11 lines including the line being executed)
    • u: navigate up a stack frame
    • d: navigate down a stack frame
    • p: to print the value of an expression in the current context

    If you don't want to use a command line debugger, some IDEs like Pydev, Wing IDE or PyCharm have a GUI debugger. Wing and PyCharm are commercial products, but Wing has a free "Personal" edition, and PyCharm has a free community edition.

    0 讨论(0)
  • 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<tab> 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.

    0 讨论(0)
  • 2020-11-22 11:29

    Visual Studio with PTVS could be an option for you: http://www.hanselman.com/blog/OneOfMicrosoftsBestKeptSecretsPythonToolsForVisualStudioPTVS.aspx

    0 讨论(0)
  • 2020-11-22 11:30

    By using Python Interactive Debugger 'pdb'

    First step is to make the Python interpreter to enter into the debugging mode.

    A. From the Command Line

    Most straight forward way, running from command line, of python interpreter

    $ python -m pdb scriptName.py
    > .../pdb_script.py(7)<module>()
    -> """
    (Pdb)
    

    B. Within the Interpreter

    While developing early versions of modules and to experiment it more iteratively.

    $ python
    Python 2.7 (r27:82508, Jul  3 2010, 21:12:11)
    [GCC 4.0.1 (Apple Inc. build 5493)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import pdb_script
    >>> import pdb
    >>> pdb.run('pdb_script.MyObj(5).go()')
    > <string>(1)<module>()
    (Pdb)
    

    C. From Within Your Program

    For a big project and long-running module, can start the debugging from inside the program using import pdb and set_trace() like this :

    #!/usr/bin/env python
    # encoding: utf-8
    #
    
    import pdb
    
    class MyObj(object):
        count = 5
        def __init__(self):
            self.count= 9
    
        def go(self):
            for i in range(self.count):
                pdb.set_trace()
                print i
            return
    
    if __name__ == '__main__':
        MyObj(5).go()
    

    Step-by-Step debugging to go into more internal

    1. Execute the next statement… with “n” (next)

    2. Repeating the last debugging command… with ENTER

    3. Quitting it all… with “q” (quit)

    4. Printing the value of variables… with “p” (print)

      a) p a

    5. Turning off the (Pdb) prompt… with “c” (continue)

    6. Seeing where you are… with “l” (list)

    7. Stepping into subroutines… with “s” (step into)

    8. Continuing… but just to the end of the current subroutine… with “r” (return)

    9. Assign a new value

      a) !b = "B"

    10. Set a breakpoint

      a) break linenumber

      b) break functionname

      c) break filename:linenumber

    11. Temporary breakpoint

      a) tbreak linenumber

    12. Conditional breakpoint

      a) break linenumber, condition

    Note:**All these commands should be execute from **pdb

    For in-depth knowledge, refer:-

    https://pymotw.com/2/pdb/

    https://pythonconquerstheuniverse.wordpress.com/2009/09/10/debugging-in-python/

    0 讨论(0)
  • 2020-11-22 11:31

    Python Tutor is an online single-step debugger meant for novices. You can put in code on the edit page then click "Visualize Execution" to start it running.

    Among other things, it supports:

    • live help from volunteers (which is great for newbies)
    • setting breakpoints
    • hiding variables
    • saving/sharing
    • a few other languages like Java, JS, Ruby, C, C++

    However it also doesn't support a lot of things, for example:

    • Reading/writing files - use io.StringIO and io.BytesIO instead: demo
    • Code that is too large, runs too long, or defines too many variables or objects
    • Command-line arguments
    • Lots of standard library modules like argparse, csv, enum, html, os, struct, weakref...
    0 讨论(0)
  • 2020-11-22 11:34

    There exist breakpoint() method nowadays, which replaces import pdb; pdb.set_trace().

    It also has several new features, such as possible environment variables.

    0 讨论(0)
提交回复
热议问题