所以我想了一下,但是没有找到任何有关我的问题的信息,所以我决定打开一个新问题。
我的问题是,如果用户要导入已重命名的模块,如何在python中引发DeprecationError?
我将要完成的快速示例:
>>> import foo.bar as foobar
DeprecationWarning: import foo.bar is deprecated and will be removed in a
future release, please use import foo.bar2 in order to access the goodness
of foo bar
我尝试在bar文件中执行以下操作(对于本示例):
bar.py
import stuff
warnings.warn("import foo.bar is deprecated and will be removed in a future release, please use import foo.bar2 in order to access the goodness of foo bar", DeprecationWarning)
但是不幸的是,当我运行测试脚本并导入模块时,此方法不起作用。有什么我想念的吗?加载模块后如何在模块中运行代码?
答案 0 :(得分:1)
您需要为警告设置过滤器,或将-Wd开关与解释器调用一起使用。默认情况下,弃用警告处于关闭状态。
在http://www.jaggedverge.com/2016/09/deprecation-warnings-in-python/上查看示例