Question

Edit: the linked post does not solve this problem, this is still an outstanding issue.

I execute the following program in Spyder with iPython:

import time

print "Please enter your name."
userName=raw_input();
print "Now let's wait a few seconds, {}.".format(userName)
time.sleep(1)
print "Did you lose your patience?"

It prompts the user for their name, then (instead of printing the first line, waiting, and then printing the second line), it pauses and then prints the outputs of the last two print statements at the same time.

When I run from the command line, it works as expected. So does anyone know what I can do so that the script shows the desired behavior from within Spyder/iPython (I am working in Windows 7, Spyder 2.2.5 running iPython with Python 2.7).

Note because I get the expected behavior when I run from the command line, the suggestion at Why is time.sleep pausing early? is not transparently applicable, but perhaps it is easy to port that solution to this case? Also, running 'sys.stdout.flush()' before the sleep command doesn't seem to do anything.

Was it helpful?

Solution

I tested this in Spyder and vanza's solution works for me. Are you sure you put sys.stdout.flush() in the right line? It should look like this:

vanza's solution:

import time
import sys

print "Please enter your name."
userName=raw_input();
print "Now let's wait a few seconds, {}.".format(userName)
sys.stdout.flush() # <- *** it goes here ***
time.sleep(3)
print "Did you lose your patience?"

You might want to scroll down the iPython console a bit before entering your input, to see the text clearly (otherwise it stays a bit low to be noticed).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top