I also think something along the lines of execfile() may serve the original poster. There was a thread last month about compile() and exec() with a concise example from Fredrik Lundh. Google "Changing an AST" in this group. With dynamically generated code I prefer the separate compile() step so that I can catch the compile exceptions separately.