Intellij Idea中的Python输出不秩序

发布于 2025-01-18 22:25:33 字数 1457 浏览 4 评论 0原文

我有一个非常简单的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木格 2025-01-25 22:25:33

坎普的解释是正确的。

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...).

你穿错了嫁妆 2025-01-25 22:25:33

这里的问题是打印输出转到stdout,而异常消息转到stderr。在您的终端中,他们会获得输出,但出于效率原因,IDE可能会在stdout上进行一些缓冲。相同的缓冲区不会适用于stderr,因为它需要确保输出尽快进入屏幕,而不会过早清除缓冲区。这意味着有时(经常根据我的经验)像您这样的例外消息会出现 正常输出之前,您期望已经显示出来。

The issue here is that the print output goes to stdout while the exception message goes to stderr. In your terminal they get output as-is, but the IDE is likely doing some buffering on stdout for efficiency reasons. The same buffer won't apply to stderr 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文