61

I had the same issue as Python not working in the command line of git bash, where in Git Bash, when I type Python, it just hangs.

However, typing winpty python works perfectly.

What exactly is winpty? Why is the above command useful?

endo64
  • 2,269
  • 2
  • 27
  • 34
Nico
  • 733
  • 1
  • 6
  • 7

1 Answers1

50

winpty is A Windows software package providing an interface similar to a Unix pty-master for communicating with Windows console programs.

Winpty is a compatibility layer that allows you to run a Windows console application from a Linux terminal.

  • When you cross-compile a program for Windows from Linux, the resulting binary will be a Windows console application that can be run directly from a Linux terminal without the need for winpty.
  • However, if you build a program natively on Windows, the resulting binary will be a Windows GUI application that cannot be run from a Linux terminal.
    In order to run a Windows GUI application from a Linux terminal, you need to use winpty.

That is why you need it as described here:

The software works by starting the winpty-agent.exe process with a new, hidden console window, which bridges between the console API and terminal input/output escape codes. It polls the hidden console's screen buffer for changes and generates a corresponding stream of output.

rprichard/winpty mentions:

The package consists of a library (libwinpty) and a tool for Cygwin and MSYS for running Windows console programs in a Cygwin/MSYS pty.

As detailed in "mintty/mintty Tips":

When interacting with programs that use a native Windows API for command-line user interaction (“console mode”), a number of undesirable effects are observed; this is the pty incompatibility problem and the character encoding incompatibility problem.
This basically affects all programs not compiled in a cygwin or msys environment (and note that MinGW is not msys in this context).

As a workaround, you can use winpty as a wrapper to invoke the Windows program.


So:

Why do many tools work in plain CygWin but some of them also need winpty?

This was asked (specifically for Python) in msys2/MINGW-packages issue 2645

there's a non-zero cost associated with using winpty, and most of the time, mintty works fine anyway.
Your problem stems from trying to use mingw python which is a native windows build, and hence expects it to be used from a windows console.
mintty uses pipes for input/output, so it doesn't look like a proper terminal to most programs.

On the other hand, if you use msys2 (or cygwin) python (.e.g /usr/local/bin/python2), everything just works.

(With msys2/MINGW-packages PR 2675, python3 detects the terminal correctly)


See also "Python not working in the command line of git bash", by Gabriel Staples.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • OK…but isn't that a description of what CygWin is for? Why do many tools work in plain CygWin but some of them also need winpty? – East of Nowhere Aug 09 '19 at 15:50
  • @EastofNowhere I suspect because it is the native Windows build version of Python. See my edited answer. – VonC Aug 09 '19 at 16:04
  • So, is this accurate if I make this statement?: If the program is cross-compiled for Windows, from Linux, it does _not_ require being called through `winpty`, but if it is built natively on Windows, it _does_ require being called through `winpty`. – Gabriel Staples Aug 11 '23 at 02:35
  • 1
    @GabrielStaples Yes, that statement is accurate. Winpty is a compatibility layer that allows you to run a Windows console application from a Linux terminal. When you cross-compile a program for Windows from Linux, the resulting binary will be a Windows console application that can be run directly from a Linux terminal without the need for winpty. However, if you build a program natively on Windows, the resulting binary will be a Windows GUI application that cannot be run from a Linux terminal. In order to run a Windows GUI application from a Linux terminal, you need to use winpty. – VonC Aug 11 '23 at 05:37
  • @VonC, thank you. I think that's a better answer than your answer. Consider making that the opening to your answer. – Gabriel Staples Aug 11 '23 at 06:20
  • 1
    @GabrielStaples Thank you for your feedback. I have edited the answer accordingly. – VonC Aug 11 '23 at 08:26