title: can't find variable declared with exec within a python function
body: <p>I am trying to declare python variables using the function exec from within a function. For example:</p>
  
  <pre><code>def func():
      varnames = ['x', 'y', 'z']
      vals = [5,5,'abc']
      exec(varnames[0] + '=vals[0]')
      print(x)
  
  
  func()
  </code></pre>
  
  <p>results in the error:</p>
  
  <pre><code>NameError: global name 'x' is not defined
  </code></pre>
  
  <p>However, it is indeed the case that 'x' is in locals()</p>
  
  <pre><code>def func():
      varnames = ['x', 'y', 'z']
      vals = [5,5,'abc']
      exec(varnames[0] + '=vals[0]')
      print(locals())
  
  
  func()
  </code></pre>
  
  <p>results in:</p>
  
  <pre><code>{'vals': [5, 5, 'abc'], 'x': 5, 'varnames': ['x', 'y', 'z']}
  </code></pre>
  
  <p>showing that x is present in the local namespace. Any idea why this is happening or how I can get around it?</p>