Python 如何使用 warnings.filterwarnings 抑制第三方警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3920502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to suppress a third-party warning using warnings.filterwarnings
提问by Tom Nguyen
I am using Paramiko in my python code (for sftp). Everything works fine except that everytime I import or call a paramiko function. This warning would show up:
我在我的 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)
I know that this has to do with the fact that Paramiko is using some Deprecated functionalities of PyCrypto.
我知道这与 Paramiko 正在使用 PyCrypto 的一些已弃用功能有关。
My question is, is there a way to suppress this warning programmatically ? I have tried this:
我的问题是,有没有办法以编程方式抑制此警告?我试过这个:
warnings.filterwarnings(action='ignore', \
category=DeprecationWarning, module='paramiko')
and even this:
甚至这个:
warnings.filterwarnings(action='ignore', \
category=DeprecationWarning, module='randpool')
before 'import paramiko' statement and before paramiko-specific function calls, but nothing works. This warning keeps showing up no matter what. If it helps, here's the code in the third party library that prints the warning:
在 'import paramiko' 语句之前和 paramiko 特定的函数调用之前,但没有任何效果。无论如何,此警告都会不断出现。如果有帮助,这里是打印警告的第三方库中的代码:
in randpool.py:
在 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)
If you know a way around this, please help me shut this warning off.
如果您知道解决此问题的方法,请帮助我关闭此警告。
回答by snapshoe
回答by Kyle
To filter only a specific warning:
仅过滤特定警告:
with warnings.catch_warnings():
warnings.simplefilter('ignore', SpecificWarningObject)
#do something that raises a Warning
回答by Blaine Rogers
The moduleargument to warnings.filterwarningstakes a case-sensitive regular expression which should match the fully qualified module name, so
的module到参数warnings.filterwarnings需要区分大小写的正则表达式,应当匹配完全合格的模块名,所以
warnings.filterwarnings(
action='ignore',
category=DeprecationWarning,
module=r'.*randpool'
)
or
或者
warnings.filterwarnings(
action='ignore',
category=DeprecationWarning,
module=r'Crypto\.Utils\.randpool'
)
should work. You may need to write RandomPool_DeprecationWarningexplicitly instead of DeprecationWarningif for some reason RandomPool_DeprecationWarningis not a subclass of DeprecationWarning.
应该管用。您可能需要RandomPool_DeprecationWarning显式编写而不是DeprecationWarningif 由于某种原因RandomPool_DeprecationWarning不是DeprecationWarning.
You can also disable the warning on the command line when you invoke the script by passing the -Woption to the interpreter like so:
您还可以在调用脚本时通过将-W选项传递给解释器来禁用命令行上的警告,如下所示:
$ python -W ignore::RandomPool_DeprecationWarning:Crypto.Utils.randpool: my_script.py
The -Wtakes filters in the format action:message:category:module:lineno, where this time modulemust exactly match the (fully-qualified) module name where the warning is raised.
-W格式为的take 过滤器,action:message:category:module:lineno此时module必须与引发警告的(完全限定的)模块名称完全匹配。
See https://docs.python.org/2/library/warnings.html?highlight=warnings#the-warnings-filterand https://docs.python.org/2/using/cmdline.html#cmdoption-w
请参阅https://docs.python.org/2/library/warnings.html?highlight=warnings#the-warnings-filter和https://docs.python.org/2/using/cmdline.html#cmdoption-w

