gpt4 book ai didi

python - 你怎么能找到python从哪里导入了一个特定的模块?

转载 作者:行者123 更新时间:2023-11-28 18:42:08 25 4
gpt4 key购买 nike

如何找到 python 从何处导入特定模块?

最佳答案

每个模块对象都有一个__file__属性:

import module

print module.__file__

一些模块是 Python 可执行文件的一部分;这些将没有属性集。

演示:

>>> import urllib2
>>> urllib2.__file__
'/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/urllib2.pyc'
>>> import sys
>>> sys.__file__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__file__'

您还可以在详细 模式下运行 Python,使用 -v command line switchPYTHONVERBOSE environment variable ;然后 Python 打印出每个导入的文件

关于python - 你怎么能找到python从哪里导入了一个特定的模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24941665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com