如何通過“ import”語句確定在Python中導入了哪個文件?
想確定正在加載本地修改的.py文件的正確版本。基本上等同于POSIX環(huán)境中的“哪個”。
解決方案
使用-v參數(shù)啟動python 以啟用調(diào)試輸出。當您導入模塊時,Python將打印出模塊從以下位置導入的位置:
$ python -v ... >>> import re # /usr/lib/python2.6/re.pyc matches /usr/lib/python2.6/re.py import re # precompiled from /usr/lib/python2.6/re.pyc ...
如果您還想查看Python在其他什么地方搜索了該模塊,請?zhí)砑拥诙€-v:
$ python -v -v ... >>> import re # trying re.so # trying remodule.so # trying re.py # trying re.pyc # trying /usr/lib/python2.6/re.so # trying /usr/lib/python2.6/remodule.so # trying /usr/lib/python2.6/re.py # /usr/lib/python2.6/re.pyc matches /usr/lib/python2.6/re.py import re # precompiled from /usr/lib/python2.6/re.pyc ...