Output does not redirect in expected order

When I redirect stdout to a log file, ROOT output always appears at the top of the file, regardless of its sequence in the script. If I don’t redirect it, it appears where I would expect. Is there some kind of shell magic to get the output to appear where it logically should? Reproducer:


test.py (42 Bytes)

python test.py

output:

first

RooFit v3.60 -- Developed by Wouter Verkerke and David Kirkby 
                Copyright (C) 2000-2013 NIKHEF, University of California & Stanford University
                All rights reserved, please read http://roofit.sourceforge.net/license.txt

compared with

python test.py > test.log
cat test.log

output:

RooFit v3.60 -- Developed by Wouter Verkerke and David Kirkby 
                Copyright (C) 2000-2013 NIKHEF, University of California & Stanford University
                All rights reserved, please read http://roofit.sourceforge.net/license.txt

first

ROOT Version: 6.24/06
Platform: macOS
Compiler: Not Provided


python -u ...

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