Rui L
Links: https://www.zhihu.com/question/21572891/answer/26046582
Source: Know
Copyright belongs to the author, please contact the author for authorization.
This is a rage to answer.
You should've used IPython, right? Imagine, would it be nice to automatically take you to the IPython Shell when throwing an exception? And unlike normal Ipython, this time you can call commands such as P (print), up stack, and down stack. You can also create temporary variables and execute arbitrary functions.
In fact, this is achievable and simple, but you need to install IPython first. Then save the following code as ' crash_on_ipy.py '
ImportSysClassExceptionhook:Instance=NoneDef__call__(Self,*Args,**Kwargs):IfSelf.InstanceIsNone:FromIpython.coreImportUltratbSelfinstance = ultratb. Formattedtb (mode= ' Plain ' color_scheme= ' Linux ' call_pdb =1) return self instance (*args **kwargs) sys. Excepthook = exceptionhook ()
You can then import crash_on_ipy somewhere in your project code.
This method does not require the IDE. To function like GDB, you can use the PDB, for example:
Import PDB
Pdb.set_trace ()
Add the above 2 lines to the code that needs to add a breakpoint, run, the execution can be interrupted here, one step, continue, view variable values and other functions have, may wish to help under. Dong Weiming
Links: https://www.zhihu.com/question/21572891/answer/123220574
Source: Know
Copyright belongs to the author, please contact the author for authorization.
See LZ means to understand the problem of how to debug the bug. There are 2 types of bugs:
First Kind, directly caused the error, the program throws an exception。 The upstairs has spoken of Ipython, yes. First of all, I'll write a problematic example:
a = 1b = 0a / b
The execution must be an error:
? python test.pyTraceback (most recent call last): File "test.py", line 4, in <module> a / bZeroDivisionError: integer division or modulo by zero
Someone with a bit of experience knows that the denominator is caused by 0 at a glance. But the script execution is over, and if the debug has to continue to add print to the corresponding location. The trick is:
? ipython test.py --pdb---------------------------------------------------------------------------ZeroDivisionError Traceback (most recent call last)/Users/dongweiming/test/test.py in <module>() 2 b = 0 3 ----> 4 a / bZeroDivisionError: integer division or modulo by zero*** NameError: name ‘pdb‘ is not defined> /Users/dongweiming/test/test.py(4)<module>() 1 a = 1 2 b = 0 3 ----> 4 a / bipdb> p b
The program runs in the wrong place, Ga. Stop, save the error context, into the PDB environment, direct debugging go, don't be too happy.
Speaking of which, IPDB (PDB) can set breakpoints, step through debugging, enter function debugging, view the current code, view stack fragments, dynamically change the value of variables, and so on. It has a number of shortcut keys:
ipdb> helpDocumented commands (type help <topic>):========================================EOF bt cont enable jump pdef psource run unt a c continue exit l pdoc q s until alias cl d h list pfile quit step up args clear debug help n pinfo r tbreak w b commands disable ignore next pinfo2 restart u whatisbreak condition down j p pp return unalias
Among them up,down,n,j,l,where,s, args and so on I am very commonly used, I highly recommend you each shortcut key to understand. Of course very lazy, you also have benefits, look at the Python code debugging skills.
The second type:Hide Bugs, that is, there is no error, but the output does not meet the expectations, this is more annoying, because if you have less experience when writing and do not concentrate, basically have to go to the local to confirm, some people say, "import pdb Pdb.set_trace ()", ah very standard plan, but I generally do not. What is the reason, such as debugging a Web application, if Set_trace (), it takes more than one next to get to the point where you want to debug your fingers. So I generally use the following three methods:
1. Throw exceptions. Directly let you want to debug the location let it first run an exception, such as Flask debug mode, Werkzeug inside the debuggedapplication will render the Web page as a debugging and executable environment, directly to the above debugging:
2.use print and logging in the corresponding location. This is the most basic play. I usually only in the psychological have a number, just need to look at the log output to confirm when added temporary. The usual application log will also have regular records, and will record the stack (of course, using sentry and the like to collect logs is the best), such as the important on-line process, out of the question but the development environment is not good to simulate the time, "tail-f" log files, so that the problem appears to see. Here is another interesting item to recommend: github-zestyping/q: Quick and dirty debugging output for tired programmers. , it was found in my pycon2013 speech, and was interested to see, pyvideo.org Lightning Talks. I used it before.
3.maintain some libraries for debugging yourself. I will collect the useful functions and methods used in the work, and put them in a library. There's a function that gets the call stack like this:
import sysdef get_cur_info(): print sys._getframe().f_code.co_filename # 当前文件名 print sys._getframe(0).f_code.co_name # 当前函数名 print sys._getframe(1).f_code.co_name # 调用该函数的函数的名字,如果没有被调用,则返回module print sys._getframe().f_lineno # 当前行号
You can help you pull out the hidden worm by looking at the output of the call stack of the current context.
How does the Python program debug effectively?