title: How to compile a string of Python code into a module whose functions can be called?
body: <p>In Python, I have a string of some Python source code containing functions like:</p>
  
  <pre><code>mySrc = '''
  def foo():
      print("foo")
  
  def bar():
      print("bar")
  '''
  </code></pre>
  
  <p>I'd like to compile this string into some kind of <strong>module-like object</strong> so I can call the functions contained in the code. </p>
  
  <p>Here's pseudo-code for what I'd like to do:</p>
  
  <pre><code>myMod = myCompile(mySrc)
  myMod.foo()
  </code></pre>
  
  <p>Is this possible in Python? I've tried this, but it does not work:</p>
  
  <pre><code>myMod = compile(mySrc, '', 'exec')
  myMod.foo()
  </code></pre>
  
  <p>This produces an error message like this:</p>
  
  <pre><code>&lt;code object &lt;module&gt; at 0x104154730, file "", line 1&gt;Traceback (most recent call last):
    File "myfile.py", line XX, in run
      myMod.foo()
  AttributeError: 'code' object has no attribute 'foo'
  </code></pre>