如何在用户导入模块时抛出 DeprecationWarning?
How to throw DeprecationWarning when users are importing a module?
所以我找了一下,但没有找到任何关于我的问题的内容,所以我决定提出一个新问题。
我的问题是,如果用户要导入已重命名的模块,我如何在 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)
但不幸的是,当我 运行 测试脚本并导入模块时,这不起作用。有什么我想念的吗?如何 运行 加载模块时模块中的代码?
您需要为警告设置过滤器或使用 -Wd 开关与解释器调用。默认情况下,弃用警告是关闭的。
查看示例 http://www.jaggedverge.com/2016/09/deprecation-warnings-in-python/
所以我找了一下,但没有找到任何关于我的问题的内容,所以我决定提出一个新问题。
我的问题是,如果用户要导入已重命名的模块,我如何在 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)
但不幸的是,当我 运行 测试脚本并导入模块时,这不起作用。有什么我想念的吗?如何 运行 加载模块时模块中的代码?
您需要为警告设置过滤器或使用 -Wd 开关与解释器调用。默认情况下,弃用警告是关闭的。
查看示例 http://www.jaggedverge.com/2016/09/deprecation-warnings-in-python/