Flush print on Paraview Python Shell

While running python code in the Paraview Python Shell, I found that the print output is flushed only after all the statements are completed.
For example I have this simple loop that prints something:

for i in range(0, 10):
print(i)
sleep(1)

The shell freezes for 10 seconds, and print out everything at once, while I would expect the shell prints them out each second.

This eventually affects my python script, as I am running something that takes time, and I would like to print out the progress using print function.

I think this question is more about Python than ParaView. Like many I/O systems, the Python I/O system implementing print uses buffering. So things sent to print may not be actually printed right away but may sit in a buffer to be queued later.

You can find information about flushing Python’s print buffer in other Python forms such as this Stack Overflow post.

The short answer is that you can use sys.stdout.flush() to force what you sent to print to be outputted to the console. It would look something like this:

import sys

for i in range(0, 10):
    print(i)
    sys.stdout.flush()
    sleep(1)

Thanks for the reply.

However, I did try to flush using the method you explained and print(i, flush=True), both do not mitigate the issue. The shell still shows the output after the end of the execution. During the waiting time, the shell is not responding.