PyCharm Run --- 为什么有时显示回溯和错误,有时不显示? [英] PyCharm Run --- why does it sometimes display a traceback and error and sometimes not?

查看:73
本文介绍了PyCharm Run --- 为什么有时显示回溯和错误,有时不显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,如果我在 PyCharm 中运行完全相同的 Python 3 代码,运行"选项卡有时会显示完整的回溯和错误消息,但其他时候只会显示进程已完成,退出代码为 1".

I've noticed that if I run the exact same piece of Python 3 code in PyCharm, the "Run" tab sometimes will show the full traceback and error message, but other times will only show "Process finished with exit code 1".

1) 有人可以解释为什么会发生这种情况吗?

1) Can someone explain why this happens?

2) 有没有办法强制 PyCharm 始终显示完整的回溯和错误消息?

2) Is there some way to force PyCharm to always show the full traceback and error message?

这是一个示例,显示了我一直看到的行为.当我运行这 20 次时,我得到了两次回溯和错误以及 18 次退出代码 1"消息.为了看到这种行为,代码似乎需要在引发异常之前忙于打印某些内容.如果我只引发异常(没有调用 callme()),我总是会得到回溯和错误.

Here is an example that shows the behavior that I've been seeing. When I ran this 20 times, I got the traceback and error twice and the "exit code 1" message by itself 18 times. In order to see this behavior, the code seems to need to be busy printing something before the exception is raised. If I only raise the exception (without the calling callme()) I always get the traceback and error.

#!/usr/bin/python3

def callme():
    print('doing some stuff!')

def main():
    for i in range(1,100):
        callme()
    raise Exception('Something is went wrong!')

if __name__ == '__main__':
    main()

编辑 2:我刚刚意识到这个问题的答案可能必须是.PyCharm Run 显示窗口只显示一定数量的行.如果它可以打印的行数少于我正在打印的内容量,那么某些行将不会显示.所以,实际上回溯线仍然存在,但它们只是在其他一些输出之前打印出来,所以它们在窗口中不可见.我发现通过按下运行"窗口旁边的向上小按钮,我实际上可以跳到它们(即使它们不可见).

Edit 2: I just realized that the answer to this probably has to be. The PyCharm Run display window only shows a certain number of lines. If the number of lines that it can print is less than the amount of stuff I am printing, then some of the lines won't show. So, actually the traceback lines are still there, but they just got printed way before some of the other output and so they aren't visible in the window. I found out that I can actually jump up to them (even if they aren't visible) by pressing the little up button next to the Run window.

编辑 3:看起来我不是唯一遇到此问题的人:PyCharm 输出错误消息散布在控制台输出中.如何解决这个问题?

Edit 3: Looks like I'm not the only person who has had this problem: PyCharm output error messages interspersed with console output. How to fix this?

根据那个问题,它被报告为一个错误(https://youtrack.jetbrains.com/issue/PY-16143),但是(据我所知)它没有被修复.关于如何解决这个问题的任何想法?

According to that question, it got reported as a bug (https://youtrack.jetbrains.com/issue/PY-16143), but (as far as I can tell) it wasn't repaired. Any ideas on how to get around this?

对于那些问的人:我使用的是 PyCharm 社区版 2016.3.2,我的操作系统是 CentOS 6.6.

For those who asked: I'm using PyCharm Community Edition 2016.3.2 and my operating system is CentOS 6.6.

推荐答案

我可以在 Ubuntu 16.04 PyCharm 2017.2.3 上确认此行为
如果你想每次都看到堆栈跟踪,试试这个代码,无论如何 停止使用打印进行调试 :)

I can confirm this behavior on Ubuntu 16.04 PyCharm 2017.2.3
If you want to see stack trace every time, try this code and anyway stop using print for debugging :)

import logging

def callme():
    logging.info('doing some stuff!')

def main():
    for i in range(1,100):
        callme()
    raise Exception('Something is went wrong!')

if __name__ == '__main__':
    logging.basicConfig(level='INFO')
    main()

这篇关于PyCharm Run --- 为什么有时显示回溯和错误,有时不显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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