如何声明在python中弃用的模块?
我想要在导入特定模块或调用其任何函数时打印警告。
答案 0 :(得分:7)
您希望warn
使用DeprecationWarning
。
你究竟如何调用它并不重要,但stdlib有一个不推荐使用的模块的标准模式,如下所示:
# doc string, top-level comments, imports, __all__ =
import warnings
warnings.warn("the spam module is deprecated", DeprecationWarning,
stacklevel=2)
# normal module code
有关示例,请参阅the 2.7 sets
source。