64

I've been using Cygwin on Windows recently. I want to use the Windows installation of Python, so during testing I'm using /cygdrive/c/Python26/python.exe myfile.py rather than python myfile.exe.

This is working almost perfectly, except for printing. When I run the Windows Python from Cygwin the output doesn't print until execution finishes. It works fine running in Windows Python from explorer.exe or cmd.exe, and it works in Cygwin using the Cygwin-installed Python (/bin/python.exe).

Is there a workaround for this? The important thing is to be able to run the Windows version, but I'd like to do it all from with Bash.

Skilldrick
  • 69,215
  • 34
  • 177
  • 229
  • 1
    did you try launching the bash.exe from a DOS prompt ? I think this will solve your problems without having the need to use the -i option or the PYTHONUNBUFFERED variable. Do let me know if it works for you. – fixxxer Dec 01 '12 at 19:41
  • Related : https://stackoverflow.com/questions/13588454/invoking-python-under-cygwin-on-windows-hangs – Gwen Jul 12 '17 at 14:36

3 Answers3

118

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
noisygecko
  • 1,771
  • 3
  • 15
  • 13
  • Great answer. By the way, I don't have a `Python27/Scripts/ipython.exe` in my installation... – wap26 Jul 06 '12 at 09:06
  • 1
    It doesn't come with Python unless you install something like the Enthought Python Distribution. You can install it for Windows using easy_install or download from here: http://pypi.python.org/pypi/ipython – noisygecko Nov 16 '12 at 22:48
  • 1
    This is very helpful. One note: if I press Ctrl+C to kill the program, in mintty I still don't see the traceback. With cygstart, Ctrl+C doesn't appear to work. I instead use a Cygwin bash shell running from the windows terminal (although this can't interpret some control codes properly). – Rodrigo Queiro May 14 '13 at 10:57
  • Wow, I was forced to use the """wonderful""" Microsoft cmd.exe to build our code but now, with this hint, I can use cygwin, thanks a lot! – marco Apr 24 '14 at 13:07
42

Not answering the initial question, but for those who want to use Python interactive session from within Cygwin terminal (for example in mintty) - start Python with "-i" option to tell it explicitly that it needs to run in interactive mode:

$ python -i

The neat way is also to create an alias in your .bashrc (knowing that it is only read for interactive terminal sessions anyway):

alias python='python -i'

Otherwise, Python will not know that it runs in the console, because all Cygwin pty-based terminals (mintty, rxvt and xterm) are recognized as pipes by Windows, not as the console. Therefore, Python thinks there is no console and enters non-interactive mode. So, if you still want interactive mode instead, you need to explicitly tell Python to use it. However, it still won't behave as it normally should - one still won't be able to use HOME or LEFT ARROW keys, and so on.

Tim
  • 12,318
  • 7
  • 50
  • 72
  • 5
    So why are we not able to use the HOME or LEFT ARROW keys? Is there any way to fix that? – under_the_sea_salad Nov 04 '15 at 16:17
  • 1
    @Timur : This is the answer I was looking for. Thanks for the command. Adding it to bash_profile solves the issue, but makes every call to python interactive in cygwin. Thanks again for the solution really helps. – Doogle Dec 28 '16 at 01:17
24

Perhaps if you flush the output

import sys

V = range(100000)
for x in V:
    print x
    sys.stdout.flush()
Amro
  • 123,847
  • 25
  • 243
  • 454