I'd like to know how to get the Jupyter kernel's stdout, which writes on the shell the kernel is running on, not the stdout whose output is printed to the browser. sys.stdout
only gives the latter, which is, eg. <ipykernel.iostream.OutStream at 0x17d85a28880>
.

- 155
- 1
- 6
1 Answers
Short answer: The kernel has only one sys.stdout
: the ipykernel.stdout
. Then, the input messages written in ipykernel.stdout
is sent to the proper front ends.
Long answer: First a little analogy: you can picture yourself a brain that processes computation: this is the kernel. Although, the kernel, the brain, does not perform them in its own will, it needs the cognition, an interface, to receive order and to communicate the answer: this is the console.
IPython splits the REPL (Read, Evaluate, Print, Loop) in two parts. The kernel is the evaluate part, the console is handling the rest. sys.stdout is the object that writes the display. If you use ipython
then you use the front end of IPython - ipython console. The same kernel can be used for another front end: the notebook. And so on. You can look into the IPython documentation for illustrations (and another version of this explanation).
So, we attach a frontend to the kernel, the kernel does not care about the detail of displaying (IPython, notebook etc...). The frontend receive messages from the kernel. (There is a format ZMQ of JSON message).
Important edit: You can read the code for ipykernel.iostdout
. This is a writer for the socket exchanging messages by JSON to the frontend. To be more precise, the kernel has a sys.stdout
which is only a writer for messages to be print on any frontend.

- 111
- 8
-
Thank you for your answer very much. At least on Windows, I guess every process has only one stdout at the same time; in IPython's case, do you mean ipython kernel's stdout is the `ipykernel.iostdout` object? Then, how does the kernel print some kernel-related messages on the shell where it is running, apart from the ones on the notebook(which is on the browser). I thought the shell is the genuine stdout and, `ipykernel.iostdout` from sys.stdout would be just a stdout-pretending one for convenience, particularly for the communication between the kernel and the notebook as you explained. – Smart Humanism May 06 '23 at 17:36
-
1Yes you understood correctly: ipykernel.iostdout is the only stdout for an ipython kernel. The message written is sent to a front end: notebook or ipython console (this is different of the ipython kernel). Think that sys.stdout is interpreted by the kernel so it prints its stdout. You can check ipython console or notebook to see how these messages are handled (the link provided in my answer can be a proper start). Maybe on more example can help, a single kernel can be plugged to multiple front ends. But it keeps its only stdout. The messages written are directed to the proper front end. – MufasaChan May 06 '23 at 19:31
-
Thank you for your another answer. I will look into the link you provided. I appreciate it. – Smart Humanism May 07 '23 at 05:48