Skip to content

Instantly share code, notes, and snippets.

@lunixbochs
Created December 14, 2010 01:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lunixbochs/739872 to your computer and use it in GitHub Desktop.
Save lunixbochs/739872 to your computer and use it in GitHub Desktop.
def sandbox_import(path, module):
import sys
old = sys.path
sys.path = [path]
oldmod = None
if module in sys.modules:
oldmod = sys.modules[module]
mod = __import__(module)
sys.path = old
if oldmod:
sys.modules[module] = oldmod
else:
del sys.modules[module]
return mod
def sandbox_import_namespace(path, module, scope):
mod = sandbox_import(path, module)
v = vars(mod)
for key in v:
if not key.startswith('_'):
scope[key] = v[key]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment