How to quickly debug misbehaving script in Python without pdb?

后端 未结 3 1108
感情败类
感情败类 2021-01-29 12:58

There is a high level logic error deep within my Python script, and pdb doesn\'t help to debug it. Is there any other way to see what is being executed after I run

相关标签:
3条回答
  • 2021-01-29 13:32

    I found a way to do this using excellent trace module that comes with Python.

    An example how to troubleshoot module installation problem:

    python -m trace -t setup.py install > execution.log
    

    This will dump all source line of setup.py install execution to execution.log. I find this more useful than pdb, because usability of pdb command line interface is very poor.

    0 讨论(0)
  • 2021-01-29 13:35

    No, there's no magic formula.

    My best suggestion is to get a good IDE with a debugger, like JetBrains' PyCharm, and step through your code to see where you went wrong.

    Most of the time these situations happen because you make assumptions about behavior that aren't true. Get a debugger, step through, and check your assumptions.

    0 讨论(0)
  • 2021-01-29 13:41

    I would recommend using pdb. You can use

    import pdb
    

    at the top of your script, and then add the line

    pdb.set_trace()
    

    somewhere in the code where you want to trace the problem. When the script gets to that line, you will have an interactive console where you can check variable values, run your own checks, and see what is going on. You can use n to execute the next line, or c to continue to the next occurrence of set_trace(). Full documentation is here: http://docs.python.org/2/library/pdb.html.

    Let me know if you have any specific questions!

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