Printing outputs to sdtout via nbconvert delays until whole cell completes

Hello All!

I’m using nbconvert.ExecutePreprocessor in order to execute an ipynb file.
In that execution - I’d like the outputs written/printed to the stdout in real time.

So, for example - If the first cell of the notebooks goes like this:

from time import sleep

print("hello world - please print me immediately")
sleep(10)
1/0

The desired behavior - print imeediatly, wait 10 seconds & finally crash.
The actual behavior - nothing is printed for 10 seconds & then all output of cell is displayed while crash.

My question - How can I change the actual behavior into the desired behavior?

I wonder if disabling buffering or adding a flush could help? Like in:

import sys
from time import sleep

print("hello world - please print me immediately")
sys.stdout.flush()
sleep(10)
1/0

Or setting PYTHONUNBUFFERED variable or run Python with -u (for unbufered).

Though maybe not if nbconvert only displays outputs after the cell completion :thinking:

Thanks for taking time to respond!

I’ve tried both of your suggestions - but neither changed the initial actual behavior.

Appreciate your intent to help :slightly_smiling_face: