我有一個很大的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