Suppress warnings on import?

你离开我真会死。 提交于 2019-12-25 04:15:34

问题


Assuming I write a python package that has to use the imp module, and my package is "TestModule" which is the following:

import imp
import pip
import sys

def update_and_reload(module, *args, **kwargs):
    pip.main(['install', module, '--upgrade', '--user'])
    return imp.reload(module)

When I do import TestModule in the terminal, I get a pending deprecation warning on imp. How would I make imp's warning not occur / filter out?


回答1:


Well you could use the warning module:

import warnings

with warnings.catch_warnings():
    warnings.filterwarnings("ignore", category=DeprecationWarning)
    import imp
import pip
...    


来源:https://stackoverflow.com/questions/32277931/suppress-warnings-on-import

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!