def _run_module_code(code, init_globals=None, mod_name=None, mod_fname=None, mod_loader=None, pkg_name=None):
 temp_module = imp.new_module(mod_name)
 mod_globals = temp_module.__dict__
 saved_argv0 = sys.argv[0]
 restore_module = (mod_name in sys.modules)
 if restore_module:
  saved_module = sys.modules[mod_name]
 sys.argv[0] = mod_fname
 sys.modules[mod_name] = temp_module
 try:
  _run_code(code, mod_globals, init_globals, mod_name, mod_fname, mod_loader, pkg_name)
 finally:
  sys.argv[0] = saved_argv0
  if restore_module:
   sys.modules[mod_name] = saved_module
  else:
   del sys.modules[mod_name]
 return mod_globals.copy()

def _run_module_code ( code , init_globals = None , mod_name = None , mod_fname = None , mod_loader = None , pkg_name = None ) : temp_module = imp . new_module ( mod_name ) mod_globals = temp_module . __dict__ saved_argv0 = sys . argv [ 0 ] restore_module = ( mod_name in sys . modules ) if restore_module : saved_module = sys . modules [ mod_name ] sys . argv [ 0 ] = mod_fname sys . modules [ mod_name ] = temp_module try : _run_code ( code , mod_globals , init_globals , mod_name , mod_fname , mod_loader , pkg_name ) finally : sys . argv [ 0 ] = saved_argv0 if restore_module : sys . modules [ mod_name ] = saved_module else : del sys . modules [ mod_name ] return mod_globals . copy ( )
helper to run code in new namespace with sys modified .