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

2024-04-18 02:18:35 发布

您现在位置:Python中文网/ 问答频道 /正文

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

When listing multiple filters on a single line (as for PYTHONWARNINGS), the individual filters are separated by commas

但是当我尝试这样做时,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::'

如果我跑就行了

PYTHONWARNINGS="always::::,error::RuntimeWarning::" python -c "print('hello, world')"

但是把它作为命令行标志传递的语法是什么呢?你知道吗


Tags: the警告过滤器helloworlddocumentationerrormultiple