Python call under CygWin on Windows hangs

Installing the new Windows system, I installed CygWin and 64 bit Python (2.7.3) in my default locations ( c:\cygwin and c:\Python27\python ), and added both CygWin bin and Python to my path (in the variable user PATH). From a normal command window, Python starts up fine, but when I call it from bash in the CygWin environment, it freezes, never giving input hints.

I did this on other machines before, but always with older versions of Python (32 bits) and CygWin, as well as Python in a clearly non-standard place. Has anyone else had this problem, or can someone tell me what this might be due to?

+28
python cygwin
Nov 27 '12 at 16:17
source share
7 answers

The problem is that, because the Cygwin (MinTTY) terminal behaves, the built-in Windows build in Python does not understand that stdout is a terminal device - it thinks it is a channel, an interactive mode instead of an interactive mode, and it is completely buffers its output instead of line buffering.

The reason this is new is probably because in your previous installation of Cygwin you did not have MinTTY, and the terminal used was a standard Windows terminal.

To fix this, you need to either start Python from a regular Windows terminal ( Cmd.exe ), or install a version of Cygwin Python instead of the native Windows build in Python. The version of Cygwin (installed as a package through Cygwin setup.exe ) understands the Cygwin terminals and acts properly when launched through MinTTY.

If the specific version of Python you want is not available as a Cygwin package, you can also download the Python source code and create it yourself under Cygwin. You will need a Cygwin compiler compiler if you do not already have one (GCC), but I believe that it should compile with the standard command ./configure && make && make install .

+27
Nov 27 '12 at 16:45
source share

try it

 python -i 

and yes, you will find some glitches here and there !!!

+38
Sep 07 '13 at 17:17
source share

I had a similar problem with Mercurial (hg) + OpenSSH, Python and MinTTY, but in MSYS instead of CygWin. However, as far as I can tell, both this and my problem were caused by the fact that MinTTY did not process applications that use the native functions of the Windows console (in the answer here, Adam, he explained this in detail for Python).

For me, I implemented the solution found in comment 64 https://code.google.com/p/mintty/issues/detail?id=56#c64

When the winpty project ( https://github.com/rprichard/winpty ) compiled and in my path, I was able to run native Python (in interactive mode) and Mercurial from the MinTTY shell without special assemblies or switches (e.g. python -i ). All I needed to add was console.exe or console before the python or hg command. For convenience, I added aliases such as alias hg="console.exe hg" , so I can use the same commands, whether in the Linux shell or the Windows MinTTY bash shell.

In addition, this solution works for more native applications outside of python and hg. For example, starting mysql (with or without -p ) would give the same problem (for example, "freezes" without prompting for input). Adding console to it allowed us to do this as usual.

+9
Apr 19 '14 at 0:18
source share

According to https://stackoverflow.com/a/3186269/ you can also try

 /cydrive/c/Python27/python.exe -i foo.py 
+2
Apr 15 '13 at 19:27
source share

To manage the locations of non-cygwin different versions of Python in CygWin:

 $ /usr/sbin/alternatives.exe 

Use the -install and --config options here, it works the same as update-alternatives on a Linux system. I use this along with the python -i approach and it works well.

I also had to delete the sym-link files in /usr/bin , since they were installed with the CygWin peon and were not controlled by alternatives.exe initially.

+1
Oct 01 '14 at 21:19
source share

In my solution, a shell script was written to run a python application.

 python file.py "$@" | tee /dev/null 

This extra tee command (nowhere) seems to fix the problem.

0
Apr 10 '17 at 14:40
source share

another universal workaround calls it through winpty https://github.com/rprichard/winpty

0
Nov 14 '17 at 17:10
source share



All Articles