title: Why does Python 3 exec() fail when specifying locals?
body: <p>The following executes without an error in Python 3:</p>
  
  <pre><code>code = """
  import math
  
  def func(x):
      return math.sin(x)
  
  func(10)
  """
  _globals = {}
  exec(code, _globals)
  </code></pre>
  
  <p>But if I try to capture the local variable dict as well, it fails with a <code>NameError</code>:</p>
  
  <pre><code>&gt;&gt;&gt; _globals, _locals = {}, {}
  &gt;&gt;&gt; exec(code, _globals, _locals)
  ---------------------------------------------------------------------------
  NameError                                 Traceback (most recent call last)
  &lt;ipython-input-9-aeda81bf0af1&gt; in &lt;module&gt;()
  ----&gt; 1 exec(code, {}, {})
  
  &lt;string&gt; in &lt;module&gt;()
  
  &lt;string&gt; in func(x)
  
  NameError: name 'math' is not defined
  </code></pre>
  
  <p>Why is this happening, and how can I execute this code while capturing both global and local variables?</p>