在没有__file__的情况下检查导入模块的路径

时间:2015-09-17 06:08:38

标签: python python-2.7

我正在从程序本身的某个程序中导入一个模块。 我可以运行Python命令,我需要我正在导入的python模块的路径。 该模块不包含__file__属性,这似乎是主要方式。

使用sys.modules[<module>]返回<module (built-in)>

有解决方法吗?

1 个答案:

答案 0 :(得分:1)

您可以尝试使用 inspect 提供的getfile()方法。

import inspect
inspect.getfile(inspect) # returns '/usr/lib/python2.7/inspect.pyc'