Related: [Python] Super Useful Debugging
@ Introduction to Python for science and technology calculation -Development basics, essential libraries, acceleration- by Kenji Nakahisa p80
If you use IPython (or Jupyter Notebook), "post-analysis debugging" is possible. In "post-analysis debugging", debug is run after an error occurs.
In [26]: run myscript.py
...
NameError: name 'z' is not defined
In [27]: debug
Start debugging with
It seems that you can work in the flow of running the program and debugging after noticing the error.
https://docs.python.jp/3/library/pdb.html
It also supports post-mortem debugging and can be called under program control.
Is it possible to debug post-analysis with Python itself?
pdb.post_mortem()
https://docs.python.jp/3/library/pdb.html
pdb.post_mortem(traceback=None) Enters post-parsing debugging of the given traceback object.
I found the following: https://stackoverflow.com/questions/242485/starting-python-debugger-automatically-on-error
I've tried.
Operating environment
CentOS 6.8 (64bit)
httpd.x86_64 0:2.2.15-54.el6.centos
Python 2.6.6
postdebug_170907.py
import pdb, traceback, sys
def addxy(x, y):
return z
try:
x, y = 3.141, 2.718
z = addxy(x, y)
except:
type, value, tb = sys.exc_info()
traceback.print_exc()
pdb.post_mortem(tb)
$ python postdebug_170907.py
Traceback (most recent call last):
File "postdebug_170907.py", line 8, in <module>
z = addxy(x, y)
File "postdebug_170907.py", line 4, in addxy
return z
NameError: global name 'z' is not defined
> /home/wrf/WORK/PYTHON/postdebug_170907.py(4)addxy()
-> return z
(Pdb) p x
3.141
When an error occurs, pdb starts and it seems that you can debug px etc.
It has not been used properly with -m pdb
.
Recommended Posts