开发者

discover path of python module [duplicate]

This question already has answers here: Closed 11 years ago.

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.

EDIT

Note 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__
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜