python - 在 Jupyter 中将警告转化为错误

我想在运行 Python 3 内核的 Jupyter notebook 中使用以下方法将警告转化为错误:

from warnings import warn, simplefilter
simplefilter('error')

虽然这在从命令行使用 Python 时有效,但这会使 Jupyter 崩溃。有没有人遇到过这个问题?有人知道修复方法吗?

谢谢

最佳答案

要将所有警告变成错误,您需要执行以下操作:

import warnings
warnings.filterwarnings('error')

请引用https://docs.python.org/3/library/warnings.html更多选择

default                      # Show all warnings (even those ignored by default)
ignore                       # Ignore all warnings
error                        # Convert all warnings to errors
error::ResourceWarning       # Treat ResourceWarning messages as errors
default::DeprecationWarning  # Show DeprecationWarning messages
ignore,default:::mymodule    # Only report warnings triggered by "mymodule"
error:::mymodule[.*]         # Convert warnings to errors in "mymodule"
                             # and any subpackages of "mymodule"

https://stackoverflow.com/questions/67801782/

相关文章:

python - Pydantic Enum 加载名称而不是值

java - 打开Feign java.lang.NoSuchFieldError : MULTIP

javascript - 将 HTML 内容转换为字符串并在文本文件中打印

node.js - 在 NodeJs 中使用 Docker-compose 热重载

reactjs - 当将组件作为函数调用而不是将其用作标签时,React 没有相同的执行顺序?

python - odoo 13 定制多家公司的产品和供应商

.net-core - 仅禁用与 HealthCheck 相关的日志记录

typescript - 如何强制两种类型在 TypeScript 中不兼容?

android - 如何更改android studio中的默认主题?

javascript - 如何测试 Chrome 扩展程序的 chrome.runtime.onIn