@contextmanager
def temporary_unloaded_module(python_file_contents):
 with tempfile.NamedTemporaryFile(dir='test/', prefix='_test_time_generated_module', suffix='.py') as temp_module_file:
  temp_module_file.file.write(python_file_contents)
  temp_module_file.file.flush()
  temp_module_path = temp_module_file.name
  temp_module_name = re.search('/(_test_time_generated_module.*).py', temp_module_path).group(1)
  (yield temp_module_name)

@ contextmanager def temporary_unloaded_module ( python_file_contents ) : with tempfile . NamedTemporaryFile ( dir = 'test/' , prefix = '_test_time_generated_module' , suffix = '.py' ) as temp_module_file : temp_module_file . file . write ( python_file_contents ) temp_module_file . file . flush ( ) temp_module_path = temp_module_file . name temp_module_name = re . search ( '/(_test_time_generated_module.*).py' , temp_module_path ) . group ( 1 ) ( yield temp_module_name )
create an importable module return the name of importable module name given its file contents .