@contextlib.contextmanager
def mock_module_import(module):
 parts = module.split('.')
 entries = ['.'.join(parts[:(i + 1)]) for i in range(len(parts))]
 for entry in entries:
  sys.modules[entry] = object()
 try:
  (yield)
 finally:
  for entry in entries:
   del sys.modules[entry]

@ contextlib . contextmanager def mock_module_import ( module ) : parts = module . split ( '.' ) entries = [ '.' . join ( parts [ : ( i + 1 ) ] ) for i in range ( len ( parts ) ) ] for entry in entries : sys . modules [ entry ] = object ( ) try : ( yield ) finally : for entry in entries : del sys . modules [ entry ]
place a dummy objects in sys .