Python doesn't have a built-in way to run untrusted code safely. Multiple attempts have been made, but none really succeeded.
Why? Because Python is a highly introspective object-oriented language with a mutable runtime. Core elements of the interpreter can be accessed through the object graph, frames and tracebacks, making runtime isolation difficult. This means that even aggressive restrictions can be bypassed:
# Attempt: Remove dangerous built-ins
del __builtins__.eval