ipython / ipython/ipykernel

ipykernel 6.x breaks redirecting stdout

Aberta
#795 8 comentários 6 reações 0 responsáveis Ver no GitHub
Linguagem predominante
Python
Estrelas
734
Forks
412
Merge médio
1d 5h
PRs com merge (30d)
8

Descrição

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.

Guia de contribuição

Abrir o guia de contribuição

Avaliação

Esta issue ainda não foi avaliada.

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.