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