title: Python, import string of Python code as module
body: <p>In python you can do something like this to import a module using a string filename, and assign its namespace a variable on the local namespace.</p>
  
  <pre><code>x = __import__(str)
  </code></pre>
  
  <p>I'm wondering if there is a related function that will take take a string of Python code, instead of a path to a file with Python code, and return its namespace as a variable.</p>
  
  <p>For example,</p>
  
  <pre><code>str = "a = 5";
  x = importstr(str)
  print x.a
  #output is 5
  </code></pre>
  
  <p>I realize that I could write the string to a file, then use <code>__import__</code> on it, but I'd like to skip the intermediate file if possible.</p>
  
  <p>The reason for this is that I'm experimenting with metaprogramming in python, and it seems like a good solution to what I'm doing. </p>