如何将多个警告过滤器传递给 python -W?

ger*_*rit 5 python warnings

根据Python文档,我可以传递多个警告过滤器:

当在一行上列出多个过滤器时(如 PYTHONWARNINGS),各个过滤器用逗号分隔

但当我尝试这样做时,Python 抱怨它忽略了无效选项。在这里,我试图通过always::::并且error::RuntimeWarning::

$ python -W 'always::::,error::RuntimeWarning::' -c "print('hello, world')"
Invalid -W option ignored: too many fields (max 5): 'always::::,error::RuntimeWarning::'
Run Code Online (Sandbox Code Playgroud)

如果我跑的话它会起作用

PYTHONWARNINGS="always::::,error::RuntimeWarning::" python -c "print('hello, world')"
Run Code Online (Sandbox Code Playgroud)

但是将其作为命令行标志传递的语法是什么?

小智 -1

尝试将以下内容添加到您的脚本中

import warnings
warnings.filterwarnings("ignore")
Run Code Online (Sandbox Code Playgroud)

  • 我知道我可以在剧本中做到这一点。问题是如何通过命令行来完成它。 (2认同)