title: How to flush the printed statements in IPython
body: <p>I want to print some debug statements during a loop in my function and I use IPython to call the function. Let an example function be:</p>
  
  <pre><code>def test_print():
      import time
      for i in range(5):
          time.sleep(2)
          print i,  time.time()
  </code></pre>
  
  <p>The result is like follows:</p>
  
  <pre><code>0 1372337149.84
  1 1372337151.84
  2 1372337153.85
  3 1372337155.85
  4 1372337157.85
  </code></pre>
  
  <p>I expect each row to be printed, then wait for 2 seconds. But the behavior is as follows. I first observe:</p>
  
  <pre><code>0 1372337149.84
  1 
  </code></pre>
  
  <p>Then, after 2 seconds I observe the time stamp of <code>1</code> and the id of the next row, which is <code>2</code>. I observe the last time stamp finally. I couldn't figure out why it behaves like this instead of one row at a time. Any thoughts? Do I need a special flush function to print what is waiting to be printed?</p>