title: Using globals in Python exec
body: <p>I'm trying to create a custom python interpreter type application. I'm using exec statement (in Python 2.7.6) to execute given code but globals aren't working quite as expected. Could someone explain why this does not work:</p>
  
  <pre><code>def print_x():
      print(x)
  
  
  g = {'x': 10, 'print_x': print_x}
  l = {}
  
  exec('print_x()', g, l)
  </code></pre>
  
  <p>The result (whether the print_x function is in g or l), is an error:</p>
  
  <pre><code>NameError: global name 'x' is not defined
  </code></pre>
  
  <p>So, do the globals passed to exec not carry over to called functions?</p>