2

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?

prrao
  • 2,656
  • 5
  • 34
  • 39
  • Simple answer. Took me way too long to find it! http://superuser.com/questions/381942/stop-nppexec-from-trapping-console-output-until-program-finishes – prrao Apr 20 '12 at 02:22

2 Answers2

2

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.

Community
  • 1
  • 1
kindall
  • 178,883
  • 35
  • 278
  • 309
  • Using a custom class to flush output every time print is called works perfectly fine. I used Dan's custom stdout class approach from the link above. Thanks a lot! – prrao Mar 13 '12 at 14:28
1

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.

Community
  • 1
  • 1
prrao
  • 2,656
  • 5
  • 34
  • 39