Python 覆盖导入,显示导入模块的模块名称
可能重复:Python 覆盖导入,显示导入模块的模块名称,python,python-import,Python,Python Import,可能重复: 我想知道哪些模块正在导入我的示例模块“foo”: 福比 # pseudocode, this should be triggered when "foo" is imported on_import(): print "foo is imported by module X" 巴比 # this should print "foo is imported by module bar" import foo 如何实现此行为 @Wooble你说得对 这是来自中国的解决方案
我想知道哪些模块正在导入我的示例模块“foo”: 福比
# pseudocode, this should be triggered when "foo" is imported
on_import():
print "foo is imported by module X"
巴比
# this should print "foo is imported by module bar"
import foo
如何实现此行为 @Wooble你说得对
这是来自中国的解决方案
import inspect
last_frame = inspect.stack()[1]
print 'Module imported from file:line_no = %s:%i' % last_frame[1:3]