我正在 Jupyter Notebook 和我正在使用的库 (PyNN) 中运行 Python 脚本,会产生大量 stderr 输出,从而减慢代码速度并填满内存。我尝试在单元格的开头使用 %%capture 但没有任何改变,并且输出保持不变。
如有任何提示,我们将不胜感激。谢谢
如果问题与打印有关,您可以使用
contextlib
重定向流:
import contextlib
import os
devnull = open(os.devnull, 'w')
contextlib.redirect_stderr(devnull)
我使用CSS来解决方法:
%%html
<style>
div.output_stderr {
display: none;
}
</style>
您可以创建/修改您的 jupyter
_config.yml
文件以包含:
execute:
stderr-output: remove # One of 'show', 'remove', 'remove-warn', 'warn', 'error', 'severe'
您可以运行
jupyter --config-dir
来找到正确的位置。
请参阅Jupyter 的配置参考