可能重复:
know filename:line_no where an import to my_module was made
我想知道哪些模块正在导入我的示例模块“foo”:
foo.py
# pseudocode, this should be triggered when "foo" is imported
on_import():
print "foo is imported by module X"
bar.py
# this should print "foo is imported by module bar"
import foo
如何实现此行为?
答案 0 :(得分:0)
@Wooble你是对的
以下是know filename:line_no where an import to my_module was made
的解决方案import inspect
last_frame = inspect.stack()[1]
print 'Module imported from file:line_no = %s:%i' % last_frame[1:3]