title: Evaluating dynamically generated statements in Python
body: <p>I need to dynamically generate python code and execute it with eval() function.</p>
  
  <p>What I would like to do is to generate some "imports" and "assign values". I mean, I need to generate this string to evaluate it <code>eval(x)</code>.</p>
  
  <pre><code>x = """
  import testContextSummary
  import util.testGroupUtils
  testDb = [testContextSummary.TestContextSummary, 
            testGroupUtils.testGroupUtils.TestGroupUtils]
  """ # x is automatically generated
  eval(x)
  ... use testDb ...
  </code></pre>
  
  <p>I tried with this code, but eval() returns an error not recognizing <code>import</code>, so I tried this code.</p>
  
  <pre><code>x = """
  testContextSummary = __import__("testContextSummary")
  testGroupUtils = __import__("util.testGroupUtils")
  testDb = [testContextSummary.TestContextSummary, 
            testGroupUtils.testGroupUtils.TestGroupUtils]
  """ # x is automatically generated
  
  eval(x) # error
  </code></pre>
  
  <p>I again got an error not allowing assignment statement. </p>
  
  <p>Is there any way to execute dynamically generated python script, and use the result from the evalution?</p>