如何使用 warnings.filterwarnings 抑制第三方警告
我在 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)
如果您知道解决此问题的方法,请帮我关闭此警告。
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:
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.
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:
in 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最简单的方法是警告模块此处建议的那样:
Easiest way would be as the warnings module suggests here:
warnings.filterwarnings
的module
参数采用区分大小写的正则表达式,该表达式应与完全限定的模块名称匹配,因此或
应该有效。如果由于某种原因
RandomPool_DeprecationWarning
不是DeprecationWarning
的子类,您可能需要显式编写RandomPool_DeprecationWarning
而不是DeprecationWarning
。您还可以在调用脚本时通过将
-W
选项传递给解释器来禁用命令行上的警告,如下所示:-W
采用格式 < code>action:message:category:module:lineno,这次module
必须与引发警告的(完全限定)模块名称完全匹配。请参阅 https://docs.python.org/ 2/library/warnings.html?highlight=warnings#the-warnings-filter 和 https://docs.python.org/2/using/cmdline.html#cmdoption-w
The
module
argument towarnings.filterwarnings
takes a case-sensitive regular expression which should match the fully qualified module name, soor
should work. You may need to write
RandomPool_DeprecationWarning
explicitly instead ofDeprecationWarning
if for some reasonRandomPool_DeprecationWarning
is not a subclass ofDeprecationWarning
.You can also disable the warning on the command line when you invoke the script by passing the
-W
option to the interpreter like so:The
-W
takes filters in the formataction:message:category:module:lineno
, where this timemodule
must exactly match the (fully-qualified) module name where the warning is raised.See https://docs.python.org/2/library/warnings.html?highlight=warnings#the-warnings-filter and https://docs.python.org/2/using/cmdline.html#cmdoption-w
仅过滤特定警告:
To filter only a specific warning:
最灵活的方法是结合
warnings.filterwarnings()< /code>
(或
warnings.simplefilter( )
) 和警告.catch_warnings()
上下文管理器。通过这种方式,您可以获得filterwarnings
的灵活性,但过滤仅应用于with
块内:请参阅 文档中的警告过滤器,用于描述
filterwarnings
的参数。完整示例
这是带有自定义警告的完整示例:
示例输出
The most flexible way is to combine
warnings.filterwarnings()
(orwarnings.simplefilter()
) with thewarnings.catch_warnings()
context manager. This way you ge the flexibility offilterwarnings
but the filtering is only applied inside thewith
block:See The Warnings Filter in the docs for the descriptions of the arguments for
filterwarnings
.Full example
Here is a full example with a custom warning:
Example output