How can I flush the output of the print function (unbuffer python output)?

In Python 3, print can take an optional flush argument:

print("Hello, World!", flush=True)

In Python 2 you'll have to do

import sys
sys.stdout.flush()

after calling print. By default, print prints to sys.stdout (see the documentation for more about file objects).


Running python -h, I see a command line option:

-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x see man page for details on internal buffering relating to '-u'

Here is the relevant documentation.