How to debug external .py functions run from Jupyter/IPython notebook
<p>My Jupyter/IPython notebook executes functions in an external .py.</p>

<p>I need to set breakpoints within these functions, inspect variables, single step, etc.</p>

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

<p>I need some kind of workflow.</p>

<p>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?</p>

<p>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.</p>

<p>EDIT: I've found <a href="https://pypi.python.org/pypi/ipdb" rel="nofollow">https://pypi.python.org/pypi/ipdb</a> <a href="https://www.quora.com/What-are-your-favorite-tricks-for-IPython-Notebook" rel="nofollow">https://www.quora.com/What-are-your-favorite-tricks-for-IPython-Notebook</a></p>

<p>EDIT <a href="https://www.youtube.com/watch?v=Jb2HHOahvcE" rel="nofollow">https://www.youtube.com/watch?v=Jb2HHOahvcE</a> &lt;-- 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.</p>

<p>EDIT: It looks as though PyCharm does exactly what I'm after: <a href="https://www.jetbrains.com/help/pycharm/2016.1/tutorial-using-ipython-jupyter-notebook-with-pycharm.html" rel="nofollow">https://www.jetbrains.com/help/pycharm/2016.1/tutorial-using-ipython-jupyter-notebook-with-pycharm.html</a> </p>

<p>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.</p>

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

<pre><code>import pdb
pdb.set_trace()
</code></pre>

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

<p>You can refer to pdb <a href="https://docs.python.org/2/library/pdb.html#debugger-commands" rel="nofollow">documentation</a> for the operations you can perform apart from printing your variables</p>

<p><a href="http://i.stack.imgur.com/7MMia.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/7MMia.jpg" alt="PDB in Jupyter Notebook"></a></p>

python debugging ipython jupyter-notebook spyder