2013-06-04 111 views
0

可以通过os.path.pathname(sys.argv[0])获取当前脚本的路径。但是,我怎样才能得到一个模块的路径不是执行的脚本,而是脚本导入模块。我需要它读取模块的配置文件,该文件应与模块位于同一文件夹中,但模块可以安装在任何位置。获取python中模块的路径

回答

3
import os 
import your_module 

# Get directory of your module 
print os.path.dirname(your_module.__file__) 

演示:

import os 
import re 

print os.path.dirname(re.__file__) 

输出:

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7