如何使用“import”语句确定在 Python 中导入了哪个文件?
我想确定我加载的是本地修改的 .py 文件的正确版本。基本上等同于 POSIX 环境中的“which”。
最佳答案
使用-v
参数启动python 以启用调试输出。当您随后导入一个模块时,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 在其他哪些地方搜索了该模块,请添加第二个 -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
...
关于python - "import"语句如何确定在 Python 中导入了哪个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2542809/