如何在python中声明一个不推荐使用的模块

时间:2015-05-07 06:30:34

标签: python module deprecated

如何声明在python中弃用的模块?

我想要在导入特定模块或调用其任何函数时打印警告。

1 个答案:

答案 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