简体   繁体   English

使用Cygwin的Windows Python

[英]Using Windows Python from Cygwin

I've been using Cygwin on Windows recently. 我最近在Windows上使用过Cygwin。 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 . 我想使用Python的Windows安装,所以在测试期间我使用/cygdrive/c/Python26/python.exe myfile.py而不是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. 当我从Cygwin运行Windows Python时,输出在执行完成之前不会打印。 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 ). 它可以在explorer.exe或cmd.exe的Windows Python中正常运行,并且可以使用Cygwin安装的Python( /bin/python.exe )在Cygwin中运行。

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. 重要的是能够运行Windows版本,但我想用Bash完成所有操作。

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. 真正的问题是,当您在任何Cygwin终端程序(如mintty)中运行命令时,它们不会充当Windows控制台。 Only Windows Console-based ones like CMD or Console2 do that. 只有基于Windows控制台的程序(如CMD或Console2)才能执行此操作。 So, with Cygwin terminals the Windows python.exe doesn't think it is talking to an interactive console. 因此,对于Cygwin终端,Windows python.exe并不认为它正在与交互式控制台通信。

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. 这就是为什么Amro在每一行都添加了flush()来修复症状,但意味着更改代码。

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. 不改变代码的一个解决方案是使用命令行上的“-u”标志或设置PYTHONUNBUFFERED环境变量来关闭Python中的缓冲。

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. 您也无法在Cygwin终端中运行Windows python.exe交互模式。 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'(比使用'-i'选项更好):

cygstart /cygdrive/c/Python27/python.exe

And that seems to work with ipython as well (if installed): 这似乎也适用于ipython(如果已安装):

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

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: 没有回答最初的问题,但对于那些想要在Cygwin终端内使用Python交互式会话的人 (例如在mintty中) - 使用“-i”选项启动Python以明确告诉它需要以交互模式运行:

$ 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): 简洁的方法是在你的.bashrc中创建一个别名(知道它只能用于交互式终端会话):

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. 否则,Python将不知道它在控制台中运行,因为所有基于Cygwin pty的终端(mintty,rxvt和xterm)都被Windows识别为管道,而不是控制台。 Therefore, Python thinks there is no console and enters non-interactive mode. 因此,Python认为没有控制台并进入非交互模式。 So, if you still want interactive mode instead, you need to explicitly tell Python to use it. 因此,如果您仍然需要交互模式,则需要明确告诉Python使用它。 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. 但是,它仍然不会像通常那样运行 - 仍然无法使用HOME或左箭头键,依此类推。

Perhaps if you flush the output 也许你刷新输出

import sys

V = range(100000)
for x in V:
    print x
    sys.stdout.flush()

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

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