Skip to content

Instantly share code, notes, and snippets.

@fpom
Last active May 3, 2017 14:12
Show Gist options
  • Save fpom/411aa0d9c0b40df4a34c9b405daf8a4f to your computer and use it in GitHub Desktop.
Save fpom/411aa0d9c0b40df4a34c9b405daf8a4f to your computer and use it in GitHub Desktop.
Import module with standard name
import os, sys, imp
def stdimp (name) :
cwd = os.getcwd()
old = imp.new_module(name)
old.__dict__.update(sys.modules[name].__dict__)
for path in sys.path :
if path == cwd :
continue
try :
mod = imp.find_module("email", [path])
except :
continue
try :
imp.load_module(name, *mod)
new = sys.modules[name]
except :
continue
setattr(old, name, new)
sys.modules[name] = old
return old
import email
email = stdimp("email")
print "top", email
print "nested", email.email
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment