Skip to main content
 首页 » 编程设计

python之查找当前执行的是哪个库

2025年04月02日6zengkefu

我需要找出脚本中特定库的路径。

我尝试使用:

os.path.dirname(MODULE.__file__), 

但它给出了一个属性错误:

"type object 'MODULE' has no attribute  '__file__'." 

有没有其他方法可以找到特定模块的路径?我不想使用 sys.path,因为它给出了所有库的列表。

请您参考如下方法:

MODULE.__file__ 中的下划线 _ _ 之间有空格。像这样调用它:

>>> import os, operator 
>>> os.path.dirname(operator.__file__) # where MODULE is "operator" 
'/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4'