How to evaluate arbitrary code in Python?
<p>I'd like to be able to evaluate arbitrary code from a string in Python, including code composed of multiple statements, and statements that span multiple lines. The approaches I've tried so far have been to use <code>exec</code> and <code>eval</code>, but these seem to only be able to evaluate expressions. I have also tried the <a href="http://docs.python.org/library/code.html" rel="nofollow">code.InteractiveInterpreter</a> class, but this also only seems to allow evaluation using <code>eval</code>, <code>exec</code>, or single-statement modes. I've tried splitting the code by line, but this fails to handle statements that span multiple lines. Is there a way to accomplish this?</p>
