title: How can I print to console while the program is running in python?
body: <p>I have have an algorithm that I'm running that takes a while, so  I want to keep track of how far it's through, by printing to the console.</p>
  <p>So something like:</p>
  <pre><code>import sys
  
  def myMethod():
      i = 0
      while (i&lt;1000000):
          i = i+1
          output_str = str(i) + &quot;\n&quot;
          sys.stdout.write(output_str)  # same as print
          sys.stdout.flush()
      
  myMethod()
  </code></pre>
  <p>How can I have this print while it's running, rather than at the end?</p>
  <p><strong>Edit, Solution:</strong> - Posted amended code.
  This code works fine when you run it in the linux terminal using</p>
  <pre><code> python filename.py
  </code></pre>
  <p>But when I run it in Wing 101 IDE - by pressing the green play button ('Run the contents of the editor within the python shell') - it waits to till the program is finished before outputting.</p>
  <p><a href="http://wingware.com/pipermail/wingide-users/2007-April/004371.html" rel="nofollow noreferrer">Apparently it's not possible to flush stdout in the Wing IDE.</a></p>