Moh*_*med 151 python warnings ignore deprecated
我一直这样:
DeprecationWarning: integer argument expected, got float
Run Code Online (Sandbox Code Playgroud)
如何让这条消息消失?有没有办法避免Python中的警告?
Edd*_*onk 183
我有这些:
/home/eddyp/virtualenv/lib/python2.6/site-packages/Twisted-8.2.0-py2.6-linux-x86_64.egg/twisted/persisted/sob.py:12:
DeprecationWarning: the md5 module is deprecated; use hashlib instead import os, md5, sys
/home/eddyp/virtualenv/lib/python2.6/site-packages/Twisted-8.2.0-py2.6-linux-x86_64.egg/twisted/python/filepath.py:12:
DeprecationWarning: the sha module is deprecated; use the hashlib module instead import sha
Run Code Online (Sandbox Code Playgroud)
修复它:
import warnings
with warnings.catch_warnings():
warnings.filterwarnings("ignore",category=DeprecationWarning)
import md5, sha
yourcode()
Run Code Online (Sandbox Code Playgroud)
现在你仍然得到所有其他DeprecationWarning
的,但不是由以下原因造成的:
import md5, sha
Run Code Online (Sandbox Code Playgroud)
ism*_*ail 179
你应该修改你的代码,以防万一,
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
Run Code Online (Sandbox Code Playgroud)
Ste*_*202 110
从warnings
模块的文档:
#!/usr/bin/env python -W ignore::DeprecationWarning
Run Code Online (Sandbox Code Playgroud)
如果你在Windows上:-W ignore::DeprecationWarning
作为参数传递给Python.尽管通过强制转换为int来解决问题更好.
(请注意,在Python 3.2中,默认情况下会忽略弃用警告.)
Tri*_*ick 26
我发现最简洁的方法(特别是在Windows上)是通过在C:\ Python26\Lib\site-packages\sitecustomize.py中添加以下内容:
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
Run Code Online (Sandbox Code Playgroud)
请注意,我必须创建此文件.当然,如果您的路径不同,请将路径更改为python.
mak*_*kis 20
这些答案都不适合我,所以我会发布解决方法.我使用以下at the beginning of my main.py
脚本,它工作正常.
按原样使用以下内容(复制粘贴):
import "blabla"
import "blabla"
def warn(*args, **kwargs):
pass
import warnings
warnings.warn = warn
# more code here...
# more code here...
Run Code Online (Sandbox Code Playgroud)
当您只想忽略函数中的警告时,可以执行以下操作。
import warnings
from functools import wraps
def ignore_warnings(f):
@wraps(f)
def inner(*args, **kwargs):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("ignore")
response = f(*args, **kwargs)
return response
return inner
@ignore_warnings
def foo(arg1, arg2):
...
write your code here without warnings
...
@ignore_warnings
def foo2(arg1, arg2, arg3):
...
write your code here without warnings
...
Run Code Online (Sandbox Code Playgroud)
只需在要忽略所有警告的函数上添加 @ignore_warnings 装饰器
如果您使用日志记录 ( https://docs.python.org/3/library/logging.html ) 来格式化或重定向 ERROR、NOTICE 和 DEBUG 消息,则可以将 WARNINGS 从警告系统重定向到日志记录系统:
logging.captureWarnings(True)
Run Code Online (Sandbox Code Playgroud)
它将捕获带有标签“py.warnings”的警告。另外,如果您想丢弃这些警告而不记录日志,则可以使用以下命令将日志记录级别设置为 ERROR:
logging.getLogger("py.warnings").setLevel(logging.ERROR)
Run Code Online (Sandbox Code Playgroud)
它将导致所有这些警告被忽略,而不会显示在您的终端或其他任何地方。
请参阅https://docs.python.org/3/library/warnings.html和https://docs.python.org/3/library/logging.html#logging.captureWarnings并将captureWarnings 设置为 True 不会捕获警告
就我而言,我使用日志系统格式化所有异常,但警告(例如 scikit-learn)不受影响。
ENV PYTHONWARNINGS="ignore::DeprecationWarning"
Run Code Online (Sandbox Code Playgroud)
蟒蛇 3
在编写代码之前,只需编写以下易于记忆的行:
import warnings
warnings.filterwarnings("ignore")
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
138968 次 |
最近记录: |