python -u mycode.py | tee foobar.log
-u
means unbuffered standard input/output. Do check the man page, though. It has one slight catch in Python2 (also puts standard I/O into binary mode, which makes no difference in Ubuntu) and a different slight catch in Python3 (text-level I/O is still line buffered, so if your output doesn't contain many linebreaks you can still see significant buffering).