I'm using the Enthought Canopy IDE to write Python and the the output of print
commands do not reach the ipython output window at the time when I would expect them to. The clearest way to explain is to give an example:
import time
print 1
print 2
time.sleep(1)
print 3
for i in range(10):
print i
time.sleep(0.5)
The initial 1
, 2
and 3
are displayed simultaneously after a second, and then there is a half second delay before the next 0
is displayed.
(as opposed to the expected instantaneous and simultaneous display of 1
and 2
, then a 1 second delay before a simultaneous display of 3
and 0
, then half second delays)
This is causing me issues as I am looking to get readouts of variables before a time consuming portion of script runs, and I cannot get it to display these readouts without terminating the program after calling the print
command.
Any ideas/solutions?
I'm on Windows 7, let me know if you need any other specs/config details...
Thanks!
You can start the Python interpreter with an additional -u
parameter, which gives you unbuffered output (i.e. instant).
Or you can call sys.stdout.flush()
after every print
. Maybe even wrapping it in a function like this:
from __future__ import print_function
import sys
def print_flush(*args):
print(*args)
sys.stdout.flush()