Intellij Idea中的Python输出不秩序
我有一个非常简单的Python 程序。
def square(a):
for i in a:
yield i*i
def main():
a = [1, 2, 3]
b = square(a)
print(b)
print(next(b))
print(next(b))
print(next(b))
print(next(b))
if __name__ == "__main__":
main()
当我从命令行运行它时,我得到了以下结果,这正是我所期望的。
c:\test1>python main.py
<generator object square at 0x000002982E07A4A0>
1
4
9
Traceback (most recent call last):
File "main.py", line 16, in <module>
main()
File "main.py", line 12, in main
print(next(b))
StopIteration
c:\test1>
我运行了很多次,结果总是一样的。但是,当我使用 IntelliJ IDEA 和 Python 插件来运行相同的程序时,我得到了这个:
C:\Python\python.exe c:/test1/main.py
Traceback (most recent call last):
File "c:/my_projects/python1/main.py", line 16, in <module>
main()
File "c:/my_projects/python1/main.py", line 12, in main
print(next(b))
StopIteration
<generator object square at 0x000001799923C580>
1
4
9
Process finished with exit code 1
或者这个:
C:\Python\python.exe c:/test1/main.py
<generator object square at 0x00000230F551C580>
1
4
9
Traceback (most recent call last):
File "c:/my_projects/python1/main.py", line 16, in <module>
main()
File "c:/my_projects/python1/main.py", line 12, in main
print(next(b))
StopIteration
Process finished with exit code 1
为什么会发生这种情况?如何在IntelliJ IDEA中获得稳定的结果?
I have a very simple Python program.
def square(a):
for i in a:
yield i*i
def main():
a = [1, 2, 3]
b = square(a)
print(b)
print(next(b))
print(next(b))
print(next(b))
print(next(b))
if __name__ == "__main__":
main()
When I run it from the command line, I got the following result, which is what I was expecting.
c:\test1>python main.py
<generator object square at 0x000002982E07A4A0>
1
4
9
Traceback (most recent call last):
File "main.py", line 16, in <module>
main()
File "main.py", line 12, in main
print(next(b))
StopIteration
c:\test1>
I run it many times and the result is always the same. However, when I use IntelliJ IDEA with Python plugin to run the same program, I got this:
C:\Python\python.exe c:/test1/main.py
Traceback (most recent call last):
File "c:/my_projects/python1/main.py", line 16, in <module>
main()
File "c:/my_projects/python1/main.py", line 12, in main
print(next(b))
StopIteration
<generator object square at 0x000001799923C580>
1
4
9
Process finished with exit code 1
Or this:
C:\Python\python.exe c:/test1/main.py
<generator object square at 0x00000230F551C580>
1
4
9
Traceback (most recent call last):
File "c:/my_projects/python1/main.py", line 16, in <module>
main()
File "c:/my_projects/python1/main.py", line 12, in main
print(next(b))
StopIteration
Process finished with exit code 1
Why does this happen? How to get a stable result in IntelliJ IDEA?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
坎普的解释是正确的。
PyCharm 问题跟踪器中有相应的票证 https://youtrack.jetbrains.com/issue/PY -37396
另请参阅https://youtrack.jetbrains.com/issue/PY-32776
作为一种可能的解决方法,您可以为运行配置启用在输出控制台中模拟终端选项(运行 | noreferrer">https://youtrack.jetbrains.com/issue/PY-32776)编辑配置...)。
The Kemp's explanation is correct.
There is a corresponding ticket in PyCharm issue tracker https://youtrack.jetbrains.com/issue/PY-37396
See also https://youtrack.jetbrains.com/issue/PY-32776
As a possible workaround, you can enable Emulate terminal in output console option for your run configuration (Run | Edit Configurations...).
这里的问题是
打印
输出转到stdout
,而异常消息转到stderr
。在您的终端中,他们会获得输出,但出于效率原因,IDE可能会在stdout
上进行一些缓冲。相同的缓冲区不会适用于stderr
,因为它需要确保输出尽快进入屏幕,而不会过早清除缓冲区。这意味着有时(经常根据我的经验)像您这样的例外消息会出现 正常输出之前,您期望已经显示出来。The issue here is that the
print
output goes tostdout
while the exception message goes tostderr
. In your terminal they get output as-is, but the IDE is likely doing some buffering onstdout
for efficiency reasons. The same buffer won't apply tostderr
because it needs to make sure that output gets on the screen asap and without risking the buffer being prematurely cleared out. This means that sometimes (quite often in my experience) exception messages like yours will appear before normal output that you'd expect to already have been shown.