我的Python库刚刚将其主要模块名称从foo.bar
更改为foobar
。对于后向compat,foo.bar
仍然存在,但导入它会引发一些警告。现在,似乎一些示例程序仍然从旧模块导入,但不是直接导入。
我想找到错误的import
声明。是否有任何工具可以让我跟踪导入并找到罪魁祸首而不涉及所有代码?
答案 0 :(得分:52)
使用-v
启动python解释器:
$ python -v -m /usr/lib/python2.6/timeit.py
# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
# /usr/lib/python2.6/site.pyc matches /usr/lib/python2.6/site.py
import site # precompiled from /usr/lib/python2.6/site.pyc
# /usr/lib/python2.6/os.pyc matches /usr/lib/python2.6/os.py
import os # precompiled from /usr/lib/python2.6/os.pyc
import errno # builtin
import posix # builtin
# /usr/lib/python2.6/posixpath.pyc matches /usr/lib/python2.6/posixpath.py
import posixpath # precompiled from /usr/lib/python2.6/posixpath.pyc
# /usr/lib/python2.6/stat.pyc matches /usr/lib/python2.6/stat.py
import stat # precompiled from /usr/lib/python2.6/stat.pyc
# /usr/lib/python2.6/genericpath.pyc matches /usr/lib/python2.6/genericpath.py
import genericpath # precompiled from /usr/lib/python2.6/genericpath.pyc
# /usr/lib/python2.6/warnings.pyc matches /usr/lib/python2.6/warnings.py
import warnings # precompiled from /usr/lib/python2.6/warnings.pyc
# /usr/lib/python2.6/linecache.pyc matches /usr/lib/python2.6/linecache.py
import linecache # precompiled from /usr/lib/python2.6/linecache.pyc
# /usr/lib/python2.6/types.pyc matches /usr/lib/python2.6/types.py
import types # precompiled from /usr/lib/python2.6/types.pyc
# /usr/lib/python2.6/UserDict.pyc matches /usr/lib/python2.6/UserDict.py
...
然后只是grep你的旧模块。
答案 1 :(得分:5)
编辑foo.bar模块,添加以下代码:
import pdb
pdb.set_trace()
当导入foo.bar时,程序将在pdb模式下停止在pdb.set_trace(),您可以在其中调试代码。例如,您可以使用“w”命令打印完整的调用堆栈。