简体   繁体   English

将numpy警告转换为错误

[英]convert numpy warnings to errors

I am getting 我正进入(状态

C:\Anaconda\lib\site-packages\numpy\core\_methods.py:59: RuntimeWarning: Mean of empty slice.
    warnings.warn("Mean of empty slice.", RuntimeWarning)

when I run a code I wrote. 当我运行我编写的代码时。 This is a large codebase. 这是一个很大的代码库。 The problem is, I do not see where in my code this warning was fired. 问题是,我没有看到我的代码中的哪个警告被触发了。

I would rather have this as an error (eg an exception), so that I can see where this was generated in my code. 我宁愿将此作为错误(例如异常),以便我可以看到我的代码中生成的位置。

Is it possible to have exceptions instead of warnings? 是否有可能有例外而不是警告?

Note: I solved my problem debugging line by line. 注意:我逐行解决了我的问题调试。 But the question still stands. 但问题仍然存在。

Setting 设置

numpy.seterr(all='raise')

should do the trick. 应该做的伎俩。 You should probably check out the details though. 你应该查看细节

import warnings

warnings.filterwarnings('error', "Mean of empty slice.")

# ... Your code here.

See the documentation of the warnings module for more details. 有关更多详细信息,请参阅warnings模块的文档。

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

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