To clarify this is only when directing stdout to a file, printing straight to the terminal works fine. Also, I am on Mac, so no stdbuf
and the like.
I have this very straightforward file test.py
:
import time
print("start", flush=True)
time.sleep(1)
print("end")
I call it with python3 test.py > test.txt
. This works great and start
shows up followed by end
after a 1-second delay. Now I want to import NumPy:
import time
import numpy # <-
print("start", flush=True)
time.sleep(1)
print("end")
Run it again... and now, after a 1-second delay, start
and end
show up at the same time.
At this point, I have tried all the things I could imagine:
- Everything here: Disable output buffering
- Using
out = file("test.txt", "w"); out.write("start\n")
- Printing to the terminal and directing to a file with
python3 test.py 2>&1 | tee /tmp/ls.txt
Edit: I should add that interrupting the program with CTRL-C does make start
show up.