# https://dangerontheranger.blogspot.com/2012/07/how-to-use-sysmetapath-with-python.html import inspect, sys class ImaginaryModule: def division_traced(self, id, dividend, divisor): previous_frame = inspect.currentframe().f_back with open("division_traced_results.txt", 'a') as f: f.write("{}:{}:{} {} / {}\n".format( previous_frame.f_code.co_filename, previous_frame.f_lineno, id, type(dividend), type(divisor) )) return dividend / divisor class ImaginaryModuleImporter: def find_module(self, fullname, path=None): if fullname == "imaginary_module": return self def load_module(self, fullname): return ImaginaryModule() sys.meta_path.append(ImaginaryModuleImporter())