문제

One can get the path of the current script with os.path.pathname(sys.argv[0]). But how can i get the path of a module which isn't the executed script and instead the script imported the module. I need it to read a configuration file for a module which should be in the same folder as the module but the module could be installed anywhere.

도움이 되었습니까?

해결책

import os
import your_module

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

Demo:

import os
import re

print os.path.dirname(re.__file__)

Output:

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top