Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output not visible inside IPython notebook #52

Labels

Comments

@takluyver
Copy link
Contributor

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.

@erikcw
Copy link

erikcw commented Apr 7, 2014

This also seems to be the case with %qtconsole

@cangermueller
Copy link

Thank for the nice workaround! But is there meanwhile a better solution?

@gotcha
Copy link
Owner

gotcha commented Dec 22, 2014

Should be fixed by 9b2f0e5

@gotcha gotcha closed this as completed Dec 22, 2014
@sjatkins
Copy link

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

@gotcha
Copy link
Owner

gotcha commented Jan 20, 2015

@sjatkins Did you try to install ipdb from source to test 9b2f0e5 ?

@cclamb
Copy link

cclamb commented Jun 29, 2015

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.

@cclamb
Copy link

cclamb commented Jun 30, 2015

Hey guys, this still exists and should be re-opened.

@cclamb
Copy link

cclamb commented Jul 1, 2015

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.

@gotcha
Copy link
Owner

gotcha commented Jul 2, 2015

@cclamb Can you expand what you mean bout the current implementation ?

@cclamb
Copy link

cclamb commented Jul 2, 2015

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.

@gotcha
Copy link
Owner

gotcha commented Jul 6, 2015

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

@max-sixty
Copy link

+1

@gotcha
Copy link
Owner

gotcha commented Feb 23, 2016

@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

@mspacek
Copy link

mspacek commented Apr 17, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment