discover path of python module [duplicate]
Possible Duplicate:
Retrieving python module path
Suppose I have code in 3 python files. x.py, y.py, z.py.
x calls y and 开发者_JAVA技巧y calls z. In the code in z.py, I want to know what directory x.py is in.
Is there a function that will tell me this?
EDIT forget z.py. I just want y.py to print out the path of x.py.
EDITNote that the system has 30 different files all named x.py located in different directories.
If the code in z.py
has indirectly imported x.py
, it will be in sys.modules
. So try:
module = sys.modules.get('x')
if module is not None:
print module.__file__
Import x
and evaluate x.__file__
.
In this circumstance those imports may lead to chain imports. So, in some functions namespace import main module.
def where_is_x():
import __main__
print __main__.__file__
精彩评论