运行Python代码时Cmd和Git bash会有不同的结果 [英] Cmd and Git bash have a different result when run a Python code

查看:81
本文介绍了运行Python代码时Cmd和Git bash会有不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

平台:Git bash MINGW64,Windows 7、64 CMD 当我从学习Python困难方式exa 运行Python代码时.代码很简单.

Platform: Git bash MINGW64, Windows 7, 64 CMD When I run a Python code from Learn Python The Hard Way ex11. The code is simple.

print "How old are you?",
age = raw_input()
print "How tall are you?",
height = raw_input()
print "How much do you weigh?",
weight = raw_input()

print "So, you're %r old, %r tall and %r heavy." % (
    age, height, weight)

但是他们在CMD和Git bash中的结果不同.当我使用Git bash运行它时,raw_print()将首先运行.

But they have different result in CMD and Git bash. When I run it using Git bash, the raw_print() will run first.

当您输入3个答案时,最后将显示4个打印件.当我在CMD中运行它时,它正常显示,一张打印,一张raw_input().

When you input 3 answers, then it will show the 4 print in the end. When I run it in CMD, it shows normally, one print, one raw_input().

有人可以解释吗?

实际上,我的目标是解释原因,而不是用冲洗解决问题.因此,与此问题

Actually, my goal is to explain the reason, not to solve this with flush. So It is different with this question

推荐答案

因此,我对此进行了研究,并尝试了几种不同的方式来编写您所拥有的内容,并且它们的行为方式都相同.深入研究它,我遇到了 https://code.google.com/p/mintty/issues/detail?id = 218 .关键是andy.koppe的回复:

So I had a look at this and tried a couple of different ways of writing what you have there, and they all acted the same way. Digging into it some more I came across https://code.google.com/p/mintty/issues/detail?id=218. Key from this is andy.koppe's reply:

该问题的关键是stdout的默认缓冲模式取决于设备的类型:对于控制台是无缓冲的,对于管道是缓冲的.这意味着在控制台中,输出将立即显示,而在薄荷糖中,则仅在缓冲区已满或刷新后才显示,就像在main()末尾那样.

The key to the problem is that stdout's default buffering mode depends on the type of device: unbuffered for a console, buffered for a pipe. This means that in a console the output will appear immediately, whereas in mintty it will only appear once the buffer is either full or flushed, as happens at the end of main().

Windows控制台会尽快将文本打印到屏幕上,而mingw(git bash)将等到应用程序告诉它更新屏幕.

Windows Console prints text to the screen as soon as possible, while mingw (git bash) will wait until the application tells it to update the screen.

因此,要使其在两种情况下均具有相同的行为,则每次打印后都需要将缓冲区刷新到屏幕上. 如何刷新Python打印输出?包含有关如何执行操作的信息这个,但归结为以下内容:

So to get it to behave the same in both, you will need to flush the buffer to the screen after each print. How to flush output of Python print? has information about how to do this, but it comes down to the following:

import sys

print "How old are you?"
sys.stdout.flush()
age = raw_input()
print "How tall are you?"
sys.stdout.flush()
height = raw_input()
print "How much do you weigh?"
sys.stdout.flush()
weight = raw_input()

print "So, you're %r old, %r tall and %r heavy." % (age, height, weight)

或者,您可以使用-u命令在mingw中运行它,这将阻止python在mingw中缓冲输出.

Alternatively you can run it in mingw using the -u command, which will stop python from buffering output in mingw.

python -u file.py

这篇关于运行Python代码时Cmd和Git bash会有不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆