2
votes

I write Python scripts with NppExec/Notepad++. How do I update my console window as each line of my Python code is executed? For example, the following timer script:

#!usr/bin/env python
import time
import threading

class Timer(threading.Thread):
    def __init__(self, seconds):
        self.runTime = seconds
        threading.Thread.__init__(self)
    def run(self):
        counter = self.runTime
        for sec in range(self.runTime):
            print counter
            time.sleep(1.0)
            counter -= 1
        print "Done."

if __name__ == '__main__':
    t = Timer(10)
    t.start()

When I run in this in a command prompt window, it live updates every second. But in NppExec console, it updates only after exit. Is there a way to get the NppExec console to behave as the command prompt and update continuously?

2
Simple answer. Took me way too long to find it! superuser.com/questions/381942/… - prrao

2 Answers

2
votes

Not familiar with how that particular console app handles output, but stdout is buffered by default, which is probably the issue here. See this question for ways of disabling it.

1
votes

All that needs to be done is invoke the python -u command to run the script, i.e. python -u timer.py instead of python timer.py

This prints output in unbuffered mode by default. For custom output flushing in more detailed programs, the stdout definition has to be rewritten.