如何使用warnings.filterwarnings抑制第三方警告

Tom*_*yen 33 python paramiko suppress-warnings pycrypto

我在我的python代码中使用Paramiko(对于sftp).除了我每次导入或调用paramiko函数时,一切正常.此警告将显示:

C:\Python26\lib\site-packages\Crypto\Util\randpool.py:40: RandomPool_Deprecation
Warning: This application uses RandomPool, which is BROKEN in older releases.  S
ee http://www.pycrypto.org/randpool-broken
  RandomPool_DeprecationWarning)
Run Code Online (Sandbox Code Playgroud)

我知道这与Paramiko正在使用PyCrypto的一些不推荐的功能这一事实有关.

我的问题是,有没有办法以编程方式抑制此警告?我试过这个:

warnings.filterwarnings(action='ignore', \
category=DeprecationWarning, module='paramiko')
Run Code Online (Sandbox Code Playgroud)

甚至这个:

warnings.filterwarnings(action='ignore', \
category=DeprecationWarning, module='randpool')
Run Code Online (Sandbox Code Playgroud)

在'import paramiko'语句之前和paramiko特定函数调用之前,但没有任何作用.无论如何,这个警告都会出现.如果有帮助,这是第三方库中打印警告的代码:

在randpool.py中:

from Crypto.pct_warnings import RandomPool_DeprecationWarning
import Crypto.Random
import warnings

class RandomPool:
    """Deprecated.  Use Random.new() instead.

    See http://www.pycrypto.org/randpool-broken
    """
    def __init__(self, numbytes = 160, cipher=None, hash=None, file=None):
        warnings.warn("This application uses RandomPool, which is BROKEN in older releases.  See http://www.pycrypto.org/randpool-broken",
            RandomPool_DeprecationWarning)
Run Code Online (Sandbox Code Playgroud)

如果您知道解决方法,请帮我关闭此警告.

sna*_*hoe 36

最简单的方法是警告模块在这里建议:

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    import paramiko
Run Code Online (Sandbox Code Playgroud)

  • 你如何修改它只过滤OP提到的特定警告? (2认同)

Bla*_*ers 16

module到参数warnings.filterwarnings需要区分大小写的正则表达式,应当匹配完全合格的模块名,所以

warnings.filterwarnings(
    action='ignore',
    category=DeprecationWarning,
    module=r'.*randpool'
)
Run Code Online (Sandbox Code Playgroud)

或者

warnings.filterwarnings(
    action='ignore',
    category=DeprecationWarning,
    module=r'Crypto\.Utils\.randpool'
)
Run Code Online (Sandbox Code Playgroud)

应该管用。您可能需要RandomPool_DeprecationWarning显式编写而不是DeprecationWarningif 由于某种原因RandomPool_DeprecationWarning不是DeprecationWarning.

您还可以在调用脚本时通过将-W选项传递给解释器来禁用命令行上的警告,如下所示:

$ python -W ignore::RandomPool_DeprecationWarning:Crypto.Utils.randpool: my_script.py
Run Code Online (Sandbox Code Playgroud)

-W格式为的take 过滤器,action:message:category:module:lineno此时module必须与引发警告的(完全限定的)模块名称完全匹配。

请参阅https://docs.python.org/2/library/warnings.html?highlight=warnings#the-warnings-filterhttps://docs.python.org/2/using/cmdline.html#cmdoption-w


np8*_*np8 11

最灵活的方式是与上下文管理器结合warnings.filterwarnings()(或warnings.simplefilter()warnings.catch_warnings()。通过这种方式,您可以获得灵活性,filterwarnings但过滤仅应用于with块内部:

import warnings

with warnings.catch_warnings():
    warnings.filterwarnings(
        action='ignore',
        category=SomeWarningClass,
        message='some message')
   
    # Do stuff that causes the warning

Run Code Online (Sandbox Code Playgroud)

有关 的参数的描述,请参阅文档中的警告过滤器filterwarnings

完整示例

这是带有自定义警告的完整示例:

import warnings

# Custom warning class for the example
class MyWarning(UserWarning):
    pass

def function_that_issues_warning(num):
    print(num)
    warnings.warn("This is a warning", MyWarning)


with warnings.catch_warnings():
    function_that_issues_warning(1) # issues warning
    warnings.filterwarnings(
        action="ignore", category=MyWarning, message="This is a warning"
    )

    function_that_issues_warning(2) # Warning is filtered
function_that_issues_warning(3) # issues warning

Run Code Online (Sandbox Code Playgroud)

输出示例

1
.\test_warnings.py:10: MyWarning: This is a warning
  warnings.warn("This is a warning", MyWarning)
2
3
.\test_warnings.py:10: MyWarning: This is a warning
  warnings.warn("This is a warning", MyWarning)
Run Code Online (Sandbox Code Playgroud)


Kyl*_*yle 8

要仅过滤特定警告:

with warnings.catch_warnings():
    warnings.simplefilter('ignore', SpecificWarningObject)

    #do something that raises a Warning
Run Code Online (Sandbox Code Playgroud)