Python doesn't have a security model that allows you to securely execute untrusted code within your program. Even in languages that do have such security models (like Java) there tend to be countless bugs that render usage of this security model questionable.
There are two reasonable approaches to this security problem:
use security features of the operating system, e.g. seccomp/capabilities/namespaces in Linux.
The third solution is probably the most realistic: adapt your application's security model to the reality that it will run code you didn't write. E.g. if users run your software themselves, then any plugins they write are their problem. This is fine, as long as you document this possibility.
Note that merely disallowing imports is not helpful as long as you provide any user-defined objects to the untrusted code. Using reflection, it is then often possible to access any variables from the module where it was defined, including a full set of builtins or other already imported modules.