python - "import"语句如何确定在 Python 中导入了哪个文件?

标签 python import

如何使用“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/

相关文章:

python - 如何使用 urllib3 发布 x-www-form-urlencoded 数据

python - Python中十进制值的精确百分比计算

javascript - 在 Create React App 中设置导入的全局服务

python - Mac 上适用于 Python 3.2 的 Pygame - 导入错误

database - 在 SAS 中导入许多文件 .txt

python - Django 小部件覆盖模板

python - 如何从 matplotlib 中的图形中检索颜色条实例

python - 以 pythonic 方式使用 i > j ( > k) 迭代多个索引

typescript - 如何让用户从我的 NPM 包的子文件夹中导入

php - 如何将特定标签从 RSS XML 导入 MySQL 数据库