Warum schlägt Python 3 exec () bei der Angabe von Locals fehl?

Das folgende Programm wird in Python 3 fehlerfrei ausgeführt:

code = """
import math

def func(x):
    return math.sin(x)

func(10)
"""
_globals = {}
exec(code, _globals)

Aber wenn ich versuche, auch das lokale Variablendikt zu erfassen, schlägt dies mit einem @ fehNameError:

>>> _globals, _locals = {}, {}
>>> exec(code, _globals, _locals)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-9-aeda81bf0af1> in <module>()
----> 1 exec(code, {}, {})

<string> in <module>()

<string> in func(x)

NameError: name 'math' is not defined

Warum geschieht dies und wie kann ich diesen Code ausführen, während sowohl globale als auch lokale Variablen erfasst werden?

Antworten auf die Frage(2)

Ihre Antwort auf die Frage