我想找出哪些模块正在导入我的示例模块“foo”:
foo.py
# pseudocode, this should be triggered when "foo" is imported
on_import():
print "foo is imported by module X"
酒吧.py
# this should print "foo is imported by module bar"
import foo
我该如何实现这种行为?
我想找出哪些模块正在导入我的示例模块“foo”:
foo.py
# pseudocode, this should be triggered when "foo" is imported
on_import():
print "foo is imported by module X"
酒吧.py
# this should print "foo is imported by module bar"
import foo
我该如何实现这种行为?
@Wooble你是对的
这是来自know filename:line_no 的解决方案,其中对 my_module 进行了导入
import inspect
last_frame = inspect.stack()[1]
print 'Module imported from file:line_no = %s:%i' % last_frame[1:3]