Overwrite import, show names of modules importing a module
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
know filename:line_no where an import to my_module was made
我想知道哪些模块正在导入我的示例模块"foo":
P.Py
1 2 3 | # pseudocode, this should be triggered when"foo" is imported on_import(): print"foo is imported by module X" |
巴利
1 2 | # this should print"foo is imported by module bar" import foo |
如何实现此行为?
@呜呜,你说得对
以下是know filename:line_no中的解决方案,其中导入了我的_模块
1 2 3 | import inspect last_frame = inspect.stack()[1] print 'Module imported from file:line_no = %s:%i' % last_frame[1:3] |