Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug external .py functions run from Jupyter/IPython notebook

My Jupyter/IPython notebook executes functions in an external .py.

I need to set breakpoints within these functions, inspect variables, single step, etc.

It just isn't practical to use a combination of print statements and throwing exceptions to early-exit a cell.

I need some kind of workflow.

Is it possible to hook up some third-party editor/IDE to view the .py and somehow connect it to the Python runtime Jupyter/IPython is using?

So that if I set a breakpoint in my external .py using my IDE and execute a cell in the notebook which encounters said breakpoint, I can continue to navigate manually from within the IDE.

EDIT: I've found https://pypi.python.org/pypi/ipdb https://www.quora.com/What-are-your-favorite-tricks-for-IPython-Notebook

EDIT https://www.youtube.com/watch?v=Jb2HHOahvcE <-- this video is getting close to what I'm after, I just can't quite see how to put it all together. That video demonstrates spyder which is an IDE with an IPython prompt... I wonder if maybe I can run my notebook through the prompt and debug it.

EDIT: It looks as though PyCharm does exactly what I'm after: https://www.jetbrains.com/help/pycharm/2016.1/tutorial-using-ipython-jupyter-notebook-with-pycharm.html

EDIT: I'm in the middle of trying to get PyCharm to behave. I will provide the details in an answer if I sort it out.

like image 900
P i Avatar asked Sep 13 '16 08:09

P i


1 Answers

In Jupyter, you can use python debugger by adding below two lines for a breakpoint.

import pdb
pdb.set_trace()

Code execution will pause at this step and will provide you text box for debugging python code. I have attached screenshot for same.

You can refer to pdb documentation for the operations you can perform apart from printing your variables

PDB in Jupyter Notebook

like image 159
Vinod Avatar answered Sep 24 '22 12:09

Vinod