简体   繁体   English

在 Windows 上的 CygWin 下调用 python 挂起

[英]Invoking python under CygWin on Windows hangs

Installing a new Windows system, I've installed CygWin and 64 bit Python (2.7.3) in their default locations ( c:\\cygwin and c:\\Python27\\python ), and added both the CygWin bin and the Python directory to my path (in the user variable PATH).安装新的 Windows 系统,我已经在它们的默认位置( c:\\cygwinc:\\Python27\\python )安装了 CygWin 和 64 位 Python (2.7.3),并将 CygWin bin 和 Python 目录添加到我的路径(在用户变量 PATH 中)。 From the normal command window, Python starts up perfectly, but when I invoke it from bash in the CygWin environment, it hangs, never giving my the input prompt.从正常的命令窗口,Python 完美启动,但是当我在 CygWin 环境中从bash调用它时,它挂起,从不给我输入提示。

I've done this on other machines, previously, but always with older versions of Python (32 bits) and CygWin, and with Python in a decidely non-standard location.我以前在其他机器上做过这个,但总是使用旧版本的 Python(32 位)和 CygWin,并且 Python 位于明显的非标准位置。 Has anyone else had this problem, or could someone tell me what it might be due to?有没有其他人遇到过这个问题,或者有人可以告诉我这可能是什么原因造成的?

Try this试试这个

python -i

and yes you will find some glitches here and there !!!是的,你会在这里和那里发现一些小故障!!!

Option -i forces an interactive prompt as shown in Python help python -h page here.选项-i强制交互式提示,如此处的 Python help python -h页面所示。

$ python -h
-i  : inspect interactively after running script; 
      forces a prompt even if stdin does not appear to be a terminal;
      also PYTHONINSPECT=x

The problem is that due to the way that the Cygwin terminal (MinTTY) behaves, the native Windows build of Python doesn't realize that stdout is a terminal device -- it thinks it's a pipe, so it runs in non-interactive mode instead of interactive mode, and it fully buffers its output instead of line-buffering it.问题在于,由于 Cygwin 终端 (MinTTY) 的行为方式,Python 的本机 Windows 构建没有意识到 stdout 是一个终端设备——它认为它是一个管道,所以它以非交互模式运行交互模式,它完全缓冲其输出而不是行缓冲它。

The reason that this is new is likely because in your previous Cygwin installation, you didn't have MinTTY, and the terminal used was just the standard Windows terminal.这是新的原因可能是因为在您之前的 Cygwin 安装中,您没有 MinTTY,并且使用的终端只是标准的 Windows 终端。

In order to fix this, you either need to run Python from a regular Windows terminal ( Cmd.exe ), or install the Cygwin version of Python instead of a native Windows build of Python.为了解决这个问题,您需要从常规 Windows 终端 ( Cmd.exe ) 运行 Python,或者安装 Python 的 Cygwin 版本而不是 Python 的本机 Windows 版本。 The Cygwin version (installable as a package via Cygwin's setup.exe ) understands Cygwin terminals and acts appropriately when run through MinTTY. Cygwin 版本(可通过 Cygwin 的setup.exe作为包setup.exe )理解 Cygwin 终端,并在通过 MinTTY 运行时正确运行。

If the particular version of Python you want is not available as a Cygwin package, then you can also download the source code of Python and build it yourself under Cygwin.如果您想要的特定版本的 Python 没有作为 Cygwin 包提供,那么您也可以下载 Python 的源代码并在 Cygwin 下自行构建。 You'll need a Cygwin compiler toolchain if you don't already have one (GCC), but then I believe it should compile with a standard ./configure && make && make install command.如果您还没有一个 Cygwin 编译器工具链(GCC),您将需要一个 Cygwin 编译器工具链,但我相信它应该使用标准的./configure && make && make install命令./configure && make && make install编译。

I had a similar issue with Mercurial (hg)+OpenSSH, Python and MinTTY, but under MSYS instead of CygWin.我在使用 Mercurial (hg)+OpenSSH、Python 和 MinTTY 时遇到了类似的问题,但使用的是 MSYS 而不是 CygWin。 Nonetheless, as far as I can tell, both this and my issue were caused by MinTTY not being to handle applications that uses the native Windows console functions (in an answer here by Adam, he explained it in detail for Python).尽管如此,据我所知,这个问题和我的问题都是由 MinTTY 没有处理使用本机 Windows 控制台功能的应用程序引起的(在 Adam 的回答中,他详细解释了 Python)。

For me, I followed the solution found in comment 64 of https://code.google.com/p/mintty/issues/detail?id=56#c64对我来说,我遵循了https://code.google.com/p/mintty/issues/detail?id=56#c64 的评论 64 中找到的解决方案

With the winpty ( https://github.com/rprichard/winpty ) project compiled and in my path, I was able to run native Python (in interactive mode) and Mercurial from the MinTTY shell without special builds or switches (such as python -i ).编译 winpty ( https://github.com/rprichard/winpty ) 项目并在我的路径中,我能够从 MinTTY shell 运行本机Python(以交互模式)和 Mercurial,无需特殊构建或开关(例如python -i )。 All I need was to append console.exe or console before the python or hg command.我所需要的只是在pythonhg命令之前附加console.execonsole For convenience, I added aliases such as alias hg="console.exe hg" so I can use the same commands whether I'm in a Linux shell or a Windows MinTTY bash shell.为方便起见,我添加了别名,例如alias hg="console.exe hg"这样无论我是在 Linux shell 还是 Windows MinTTY bash shell 中,我都可以使用相同的命令。

Also, this solution seems to work for more native applications beyond python and hg.此外,此解决方案似乎适用于 Python 和 hg 以外的更多本机应用程序。 For example, running mysql (with or without -p ) would have given the same problem (eg "hangs" with no input prompt).例如,运行mysql (有或没有-p )会出现同样的问题(例如,没有输入提示的“挂起”)。 Appending console to it allowed it to as usual.console附加到它允许它像往常一样。

根据https://stackoverflow.com/a/9549255/745913你也可以试试

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

另一个通用的解决方法是通过winpty https://github.com/rprichard/winpty调用它,这并不是真正的 Python 特定问题。

For managing non-cygwin locations of different versions of Python in CygWin:在 CygWin 中管理不同版本 Python 的非 cygwin 位置:

$ /usr/sbin/alternatives.exe

Use the --install and --config options here, it works the same as update-alternatives on a Linux system.在这里使用 --install 和 --config 选项,它的工作方式与 Linux 系统上的update-alternatives相同。 I'm using this along with the python -i approach, and it's working well.我将它与python -i方法一起使用,并且运行良好。

I also had to delete the sym-link files in /usr/bin first, since they were installed with CygWin's python and not managed via alternatives.exe initially.我还必须首先删除/usr/bin符号链接文件,因为它们是用 CygWin 的 python 安装的,最初不是通过替代品.exe 管理的。

My solution involved writing a shell script to run the python app.我的解决方案涉及编写一个 shell 脚本来运行 python 应用程序。

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

That extra tee command (to nowhere) seems to fix the issue.那个额外的 tee 命令(无处可去)似乎解决了这个问题。

Reinstall mintty with cygwin setup.使用 cygwin 设置重新安装 mintty。 Didn't have to use python -i after that.之后不必使用 python -i 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM