Python | Jupyter> Post-mortem debugging | pdb.post_mortem ()

Related: [Python] Super Useful Debugging

Post-mortem debugging with Jupyter

@ 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.

Python post-analysis debugging

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

Python | Jupyter> Post-mortem debugging | pdb.post_mortem ()
python DS debugging
Python debugging tips
[Python] Super useful debugging
3 Jupyter notebook (Python) tricks
[Python] Debugging is more efficient!
Try running Python with Try Jupyter
Full understanding of Python debugging
python3.8 venv environment jupyter notebook
Snippet settings for python jupyter notebook
Python memo Anaconda x Jupyter Notebook
Generate Jupyter notebook ".ipynb" in Python
Easy to use Jupyter notebook (Python3.5)