禁止导入警告?

13s*_*inj 1 python warnings python-import python-interactive

假设我编写了一个必须使用该imp模块的 python 包,并且我的包是“TestModule”,如下所示:

import imp
import pip
import sys

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

当我import TestModule在终端中执行此操作时,我收到了有关 的待弃用警告imp。我如何使imp的警告不发生/过滤掉?

Igo*_*gor 5

那么你可以使用该warning模块:

import warnings

with warnings.catch_warnings():
    warnings.filterwarnings("ignore", category=DeprecationWarning)
    import imp
import pip
...    
Run Code Online (Sandbox Code Playgroud)