know filename:line_no where an import to my_module was made
我有一个模块
在模块内部,我能知道哪个文件导入了这个模块吗?我想知道这个导入的文件名:行否。
所以我需要的代码是:
Myo-模
1 | print"This module is currently imported from: file:line_no = %s:%s" % what_in_here?? |
将其放入顶层模块代码中:
1 2 3 | import traceback last_frame = traceback.extract_stack()[-2] print 'Module imported from file:line_no = %s:%i' % last_frame[:2] |
您也可以使用
1 2 3 | import inspect last_frame = inspect.stack()[1] print 'Module imported from file:line_no = %s:%i' % last_frame[1:3] |