3

The print function in Python 3 may receive a boolean value to whether flush immediately or not:

print("foobar", flush=True)

I wish to have the print function flush by default for everything it prints, is it possible? Work-around, ad-hoc settings, whatever.

Dee
  • 7,455
  • 6
  • 36
  • 70

2 Answers2

5

You can run Python in unbuffered mode with:

python -u

Or set the environment variable

PYTHONUNBUFFERED=TRUE
Benjamin Rowell
  • 1,371
  • 8
  • 16
  • this is a bit relying on the command line and not committed to git through it's good with a bash file to go with – Dee Dec 28 '20 at 10:51
3

For me this does looks like task for functools.partial, but I am not sure if it would work in this case, so can you please try following: add at begin of your file

import functools
print = functools.partial(print, flush=True)

and test if it does what you want?

Daweo
  • 31,313
  • 3
  • 12
  • 25