For code:
#!/usr/bin/python
src = \"\"\"
print \'!!!\'
import os
\"\"\"
obj = compile(src, \'\', \'exec\')
eval(obj, {\'__builtins__\': False})
The __import__
method is invoked by the import
keyword: python.org
If you want to be able to import a module you need to leave the __import__
method in the builtins:
src = """
print '!!!'
import os
"""
obj = compile(src, '', 'exec')
eval(obj, {'__builtins__': {'__import__':__builtins__.__import__}})