在我的Django应用程序,当我导入一个第三方库,我在控制台中这样的警告:无法抑制废弃警告
小鬼模块支持导入库的过时;请参阅模块的备用文档
但是,如果我在Python shell中进行导入,那么一切正常。我想在Django中实现相同的行为。这是我基于其他操作系统的线程答案尝试:在另一条错误消息,这表示,RemovedInDjango110Warning不存在
import warnings
from django.utils.deprecation import RemovedInDjango110Warning
warnings.filterwarnings(action="ignore", category=RemovedInDjango110Warning)
上面的代码的结果。我也试过这个:
import warnings
def fxn():
warnings.warn("deprecated", DeprecationWarning)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
fxn()
from third_party_lib import some_module
但是我仍然得到了非常相同的错误信息。所以,这个问题的所有以前的答案似乎都过时了。我们需要一些新的修补程序。谢谢!
我也试过这样:
import warnings
with warnings.catch_warnings():
warnings.filterwarnings("ignore",category=DeprecationWarning)
from third_party_lib import some_module
但它没有任何效果。
为什么要这么做?而不是只使用importlib? – user312016
整个故事不是关于importlib的。我正在使用一些库,也许在某处使用imp。实际上,我不想触摸这个库 – Jacobian
我只是想禁止警告,因为它在默认情况下在Python shell中完成 – Jacobian