@contextlib.contextmanager
def sys_modules_context():
 sys_modules_saved = sys.modules
 sys_modules_copy = sys.modules.copy()
 try:
  (yield)
 finally:
  sys.modules = sys_modules_saved
  sys.modules.clear()
  sys.modules.update(sys_modules_copy)

@ contextlib . contextmanager def sys_modules_context ( ) : sys_modules_saved = sys . modules sys_modules_copy = sys . modules . copy ( ) try : ( yield ) finally : sys . modules = sys_modules_saved sys . modules . clear ( ) sys . modules . update ( sys_modules_copy )
make sure sys .