如何使用"import"语句确定在Python中导入的文件?

lit*_*ost 6 python import

如何使用"import"语句确定在Python中导入的文件?

我想确定我正在加载本地修改的.py文件的正确版本.基本上相当于POSIX环境中的"which".

sth*_*sth 11

使用-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
...
Run Code Online (Sandbox Code Playgroud)

如果您还想查看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
...
Run Code Online (Sandbox Code Playgroud)


Mik*_*ham 7

看看它的__file__属性.