@contextmanager
def mask_modules(*modnames):
 realimport = builtins.__import__
 def myimp(name, *args, **kwargs):
  if (name in modnames):
   raise ImportError(('No module named %s' % name))
  else:
   return realimport(name, *args, **kwargs)
 builtins.__import__ = myimp
 (yield True)
 builtins.__import__ = realimport

@ contextmanager def mask_modules ( * modnames ) : realimport = builtins . __import__ def myimp ( name , * args , ** kwargs ) : if ( name in modnames ) : raise ImportError ( ( 'No module named %s' % name ) ) else : return realimport ( name , * args , ** kwargs ) builtins . __import__ = myimp ( yield True ) builtins . __import__ = realimport
ban some modules from being importable inside the context for example: .