Using Windows Python from Cygwin

The real problem is that when you run a command in any of the Cygwin terminal programs like mintty, they don’t act as Windows Consoles. Only Windows Console-based ones like CMD or Console2 do that. So, with Cygwin terminals the Windows python.exe doesn’t think it is talking to an interactive console.

That leads to buffering output instead of flushing buffers on every line as is done in interactive sessions. That is why Amro’s adding the flush() on every line fixes the symptom, but means changing the code.

One solution without changing the code is to turn off buffering in Python using the ‘-u’ flag on the command line or setting the PYTHONUNBUFFERED environment variable.

export PYTHONUNBUFFERED=1

/cydrive/c/Python27/python.exe foo.py

or

/cydrive/c/Python27/python.exe -u foo.py

or run in interactive mode

/cydrive/c/Python27/python.exe -i foo.py

You will also not be able to run the Windows python.exe interactive mode in the Cygwin terminal. It will not bring up an interactive session, but will just hang. I find the best solution seems to be to use ‘cygstart’ (better than using the ‘-i’ option):

cygstart /cygdrive/c/Python27/python.exe

And that seems to work with ipython as well (if installed):

cygstart /cygdrive/c/Python27/Scripts/ipython.exe

Leave a Comment