ipdb
ipdb copied to clipboard
Output not visible inside IPython notebook
When ipdb.set_trace() is called, it calls this function:
def update_stdout():
# setup stdout to ensure output is available with nose
io.stdout = sys.stdout = sys.__stdout__
This results in output being written back to the terminal where the notebook was launched, instead of appearing in the notebook itself. It will do the wrong thing in any context where you want the output to be redirected. Commenting out the call to this function makes it behave as expected.
I'm not sure what's the right heuristic to guess whether output should be redirected or not.
Reported as ipython/ipython#5247.
This also seems to be the case with %qtconsole
Thank for the nice workaround! But is there meanwhile a better solution?
Should be fixed by https://github.com/gotcha/ipdb/commit/9b2f0e59dc5133254351b2e3420aa2c54436bab5
So how do I get ipython notebook to break where I want it to? I love my notebook and %debug is fab but need more
@sjatkins Did you try to install ipdb from source to test 9b2f0e5 ?
Guys, I can see the fix in ipdb, but it tests against nose having been loaded and when I'm running ipython notebooks nose modules are in fact loaded, so the output still redirects to the console in which the notebook is running rather than the notebook itself. I've commented out the fix locally (where the code runs the test 'if 'nose' in sys.modules.keys():' prior to defining io.stdout so that io.stdout is not redefined:
if 'nose' in sys.modules.keys():
def update_stdout():
# setup stdout to ensure output is available with nose
# io.stdout = sys.stdout = sys.__stdout__
pass
This does work in my local installation, but likely causes issues with nose tests, which was the initial source of this particular bug. I could very well be doing something wrong with ipython on my end, it's a stock installation via brew on yosemite, using the latest ipython and ipdb code installed via pip.
Hey guys, this still exists and should be re-opened.
To update, ipython is not loading nose, Theano is. I've opened the above ticket with them. That said, I'm not sure that the current implementation is the best solution to this issue in ipdb either.
@cclamb Can you expand what you mean bout the current implementation ?
Sure, sorry about being unclear. My impression is that the original fix to help with nose test output was to change the stdout pointer:
def update_stdout():
# setup stdout to ensure output is available with nose
io.stdout = sys.stdout = sys.__stdout__
When that was found to cause problems in some cases (specifically with ipython notebooks), it was changed to this:
if 'nose' in sys.modules.keys():
def update_stdout():
# setup stdout to ensure output is available with nose
io.stdout = sys.stdout = sys.__stdout__
else
def update_stdout():
pass
When I say the current implementation, I mean that perhaps another fix that doesn't require contextual stdout shifting might work better. Theano's a really popular library for notebook users, and even though arguably they should not be loading nose, perhaps a fix that depends on evaluating the runtime environment in this way may be more brittle than you'd like. That said, I don't know ipdb well enough to suggest an alternative, so feel free to be a bit skeptical of my input here.
@cclamb Thanks for the detailed explanation. I agree with what you state. I was actually hoping you would already have a better idea. I'll dig deeper.
+1
@cclamb @MaximilianR
Can you try code from #84 ?
The README explains how to explicitly ask for stdout fiddling:
https://github.com/gotcha/ipdb/tree/fix_stdout#issues-with-stdout
Just to clarify, this is still a problem for me when running ipython qtconsole or jupyter qtconsole:
Jupyter QtConsole 4.2.1
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
Type "copyright", "credits" or "license" for more information.
IPython 5.3.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: 1/0
> <ipython-input-1-05c9758a9c21>(1)<module>()
----> 1 1/0
ipdb> c
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-1-05c9758a9c21> in <module>()
----> 1 1/0
ZeroDivisionError: division by zero
In [2]:
The ZeroDivisionError exception isn't printed, only after the fact when I hit c. Worse, if I run some script using run -i myscript.py in qtconsole, and it hits an exception, I get nothing at all:
In [2]: run -i myscript.py
> /home/mspacek/myscript.py(1)<module>()
----> 1 print(1/0)
ipdb> c
In [3]:
I have c.InteractiveShell.pdb = True in ipython_config.py
I've tried commenting out the update_stdout() call in ipdb.stdout, to no avail. Strangely, even when I completely uninstall ipdb (sudo pip3 uninstall ipdb), none of the above behaviour changes. Also, nothing changes if I run in Python 2.7 instead 3.5.
I'm running the latest ipdb 0.10.2 installed via pip, in Xubuntu 16.04.
I feel like this only became a problem for me recently, perhaps after moving from IPython 4 to 5, but I might be wrong about that.