如何确定要在Python中使用"import"导入哪个文件?陈述? [英] How do you determine which file is imported in Python with an "import" statement?

查看:75
本文介绍了如何确定要在Python中使用"import"导入哪个文件?陈述?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do you determine which file is imported in Python with an "import" statement?

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

I want to determine that I am loading the correct version of a locally modified .py file. Basically the equivalent of "which" in a POSIX environment.

推荐答案

使用-v参数启动python以启用调试输出.然后,您导入模块时,Python将打印出模块从以下位置导入的位置:

Start python with the -v parameter to enable debugging output. When you then import a module, Python will print out where the module was imported from:

$ 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:

If you additionally want to see in what other places Python searched for the module, add a second -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"导入哪个文件?陈述?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆