title: Output of python scripts displayed only at termination when using SSH?
body: <p>I'm running a script to manage processes on a remote (SSH) machine. Let's call it <strong>five.py</strong></p>
  
  <pre><code>#!/usr/bin/python
  import time, subprocess
  
  subprocess.call('echo 0',shell=True)
  for i in range(1,5):
     time.sleep(1)
     print(i)
  </code></pre>
  
  <p>If i now run </p>
  
  <pre><code>ssh user@host five.py
  </code></pre>
  
  <p>I would like to see the output </p>
  
  <pre><code>0
  1
  2
  3
  4
  </code></pre>
  
  <p>appear on my standard out second by second (as it does if execute locally).. What happens is: I get the 0 from "echo" right away and the rest only appears at once after the entire program finishes. (Doesn't help to nest 'five.py' into a bash script; to call it by 'python five.py'; or to use 'print >> sys.stdout, i').</p>
  
  <p>This must be related to the way python writes to stdout, since other programs behave quite normal.. A functional workaround is</p>
  
  <pre><code>import time, subprocess
  import sys
  
  subprocess.call('echo 0',shell=True)
  for i in range(1,5):
    time.sleep(1)
    sys.stdout.write(str(i)+'\n')
    sys.stdout.flush()
  </code></pre>
  
  <p>But there must be a better solution than changing all my print statements!</p>