我有一个很大的python模块库。有时,在导入模块时,我发现导入了意外的模块。为此,我一直在使用python -v
来查看哪些模块正在导入。从手册页:如何检查为什么要导入一个python模块?
-v Print a message each time a module is initialized, showing the place
(filename or built-in module) from which it is loaded. When given twice,
print a message for each file that is checked for when searching for a
module. Also provides information on module cleanup at exit.
那么,这是不正确的。例如:
import portalmq # directory /home/blahblah/python_modules/portalmq
# /home/blahblah/python_modules/portalmq/__init__.pyc matches /home/blahblah/python_modules/portalmq/__init__.py
import portalmq # precompiled from /home/blahblah/python_modules/portalmq/__init__.pyc
正如你所看到的,-v
标志只是给了我关于哪些模块是进口的,但不是其导入语句,其中文件/行触发了导入信息。使用-vv
不会改变任何东西(显示可用模块的列表,但没有说明为什么导入首先被试用)
但我需要确切地知道:哪个导入语句,其中文件/行是触发这些进口。我如何获得这些信息?
略相关:http://stackoverflow.com/questions/5983522/ – n611x007