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

69 投票
4 回答
66153 浏览
提问于 2025-04-16 05:24

我在我的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)

我知道这和Paramiko使用了PyCrypto的一些过时功能有关。

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

warnings.filterwarnings(action='ignore', \
category=DeprecationWarning, module='paramiko')

甚至还试过这个:

warnings.filterwarnings(action='ignore', \
category=DeprecationWarning, module='randpool')

在'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)

如果你知道有什么办法可以解决这个问题,请帮我关闭这个警告。

4 个回答

24

要只过滤掉特定的警告:

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

    #do something that raises a Warning
30

在使用 warnings.filterwarnings 的时候,module 参数需要一个区分大小写的正则表达式,这个表达式应该能够匹配完整的模块名称,所以

warnings.filterwarnings(
    action='ignore',
    category=DeprecationWarning,
    module=r'.*randpool'
)

或者

warnings.filterwarnings(
    action='ignore',
    category=DeprecationWarning,
    module=r'Crypto\.Utils\.randpool'
)

都可以用。你可能需要明确写出 RandomPool_DeprecationWarning,而不是 DeprecationWarning,如果出于某种原因 RandomPool_DeprecationWarning 不是 DeprecationWarning 的子类。

你还可以在命令行中禁用这个警告,当你运行脚本时,可以通过给解释器传递 -W 选项来实现,像这样:

$ python -W ignore::RandomPool_DeprecationWarning:Crypto.Utils.randpool: my_script.py

这个 -W 选项需要的格式是 action:message:category:module:lineno,这次 module 必须完全匹配发出警告的模块名称。

详细信息可以查看 这个链接这个链接

69

最简单的方法就是按照警告模块的建议,具体可以参考这里:

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    import paramiko

撰写回答