在python里,如果想产生一个警告,比如要建议调用者尽量不用某个老版本模块,写法如下:
import warnings
warnings.warn('deprecated', DeprecationWarning)
有时候我们就是需要调用三方的某个老版本模块或者函数,但是又不想看到警告,此时就需要临时性关闭警告:
import warnings
with warnings.catch_warnings():
warnings.simplefilter("ignore")
#call some deprecated functions
这个在禁止做ssl安全性检查时非常有用:
import requests
with warnings.catch_warnings():
warnings.simplefilter("ignore")
r = requests.get(url, verify=False)
否则就会出现安全性警告:
InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings