0

I'm trying to run a python script from a Windows shell (invoked via M-x shell or M-x powershell) inside Emacs.

However, no output is shown via print statements until I after I hit a raw_input statement AND provide input. In other words, the script shows no almost no output in the window until after I provide input, at which point all the output that should have been printed before get shown.

I say "almost" because LOGGER.info statements still get shown in my Emacs shell window, where LOGGER=logger.getLogger("loggerName")

For example if my script were...

LOGGER.info("Starting script")
print "Hello world."
raw_input("What's your name?").strip() 

I would see

Starting script 

And then nothing until I typed in something, at which point I would see:

Hello world.
What's your name?
1

Perhaps buffering is at play and the logger implementation flushes the buffer. Try running with unbuffered stdin, stdout, stderr:

  • python -u yourscript.py

  • PYTHONUNBUFFERED=1 yourscript.py in your environment

  • That worked! Thank you. Could I perhaps trouble you for an explanation of why that worked? – Anonymous Nov 30 '17 at 16:42
  • 1
    Hmmm, I guess I would say that buffering is a way to optimize overall throughput at the cost of latency. For example, instead of issuing separate system calls for each character, you buffer them up into a larger chunks. Then a question is when to flush the buffer: size? time? special characters (eg newlines)? never and require the user/developer to explicitly flush the buffer? In your case, the print() isn't doing any explicit flushing but in newer pythons, there is a flush option you can use if you don't want to use something as blunt as PYTHONUNBUFFERED. – Erik Stephens Feb 27 '18 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.