ipython / ipython/ipykernel

ipykernel 6.x breaks redirecting stdout

Aperta
#795 8 commenti 6 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Python
Stelle
734
Fork
412
Merge medio
1g 5h
PR unite (30g)
8

Descrizione

This issue is related to several other issues that have been reported recently, including #789, #786, #735, #737 and https://github.com/psf/black/issues/2516 but they seem to have gone unanswered over the past few months in this issue tracker. They all revolve around the fact that upgrading from ipykernel 5.x to 6.x breaks users' ability to redirect output via stdout.

As a simple reproducible example:

```python
import sys
from contextlib import redirect_stdout
terminal = sys.__stdout__
with redirect_stdout(terminal):
print("test", file=terminal, flush=True)
```

With ipykernel 5.5.5 this code redirects `print` output to the computer’s terminal window from which JupyterLab was launched, rather than outputting it to the notebook in which the code was running. However, in ipykernel 6.x, this no longer works. Now the code above directs the output to *both* the terminal and the notebook. It seems new versions of ipykernel hijack stdout via the ipykernel.iostream.OutStream object and force output to the notebook, when you used to (and presumably should, since it's very useful) be able to output directly and only to the stdout of your choice, such as the terminal.

If I downgrade ipykernel to 5.5.5 (from say 6.4.1), the old behavior is back and I am able to control where stdout goes. However, staying on v5.x is obviously not a sustainable solution. Is there a different technique in 6.x for users who need to control where stdout goes while working in an ipython Jupyter notebook? Or is this a bug that should be fixed in future ipykernel releases?

See also this [topic](https://discourse.jupyter.org/t/jupyterlab-no-longer-allows-ouput-to-be-redirected-to-stdout/11535) on Jupyter discourse and the related issues linked above from @uldz @MarcoGorelli @ItamarShDev.

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.