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

时间:2021-08-11 21:29:23

How to declare a module deprecated in python?

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

I want a warning to be printed whenever a particular module is imported or any of its functions are called.

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

1 个解决方案

#1


You want to warn with a DeprecationWarning.

你想用DeprecationWarning警告。

Exactly how you call it doesn't matter that much, but the stdlib has a standard pattern for deprecated modules, like this:

你究竟如何调用它并不重要,但stdlib有一个不推荐使用的模块的标准模式,如下所示:

# doc string, top-level comments, imports, __all__ =

import warnings
warnings.warn("the spam module is deprecated", DeprecationWarning,
              stacklevel=2)

# normal module code

See the 2.7 sets source for an example.

有关示例,请参见2.7集源。

#1


You want to warn with a DeprecationWarning.

你想用DeprecationWarning警告。

Exactly how you call it doesn't matter that much, but the stdlib has a standard pattern for deprecated modules, like this:

你究竟如何调用它并不重要,但stdlib有一个不推荐使用的模块的标准模式,如下所示:

# doc string, top-level comments, imports, __all__ =

import warnings
warnings.warn("the spam module is deprecated", DeprecationWarning,
              stacklevel=2)

# normal module code

See the 2.7 sets source for an example.

有关示例,请参见2.7集源。