我如何找出哪一行 Python 代码生成了 PendingDeprecationWarning?

How can I figure out what line of Python code is generating a PendingDeprecationWarning?

我有一组 Python 3 个单元测试,当使用此命令行执行时:

python3 -m unittest discover -f -v

...正在生成 PendingDeprecationWarning:

/usr/local/Cellar/python3/3.4.2/Frameworks/Python.framework/Versions/3.4/lib/python3.4/imp.py:32:
    PendingDeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses

有没有一种简单的方法可以让我追踪最终使用的是哪段代码 imp.py,也许是通过启用某种形式的堆栈追踪?我已经缩小了范围;它似乎只有在我使用 freezegun 时才会触发。然而,freezegun 本身似乎并没有使用 imp.

在命令行调用python时,将选项-Werror传递给它。这会将所有警告变成错误,并且当 warning/error 没有得到处理时,它将显示一个回溯,为您提供行号和每个步骤的源文件 warning/error。