Last active
May 3, 2017 14:12
-
-
Save fpom/411aa0d9c0b40df4a34c9b405daf8a4f to your computer and use it in GitHub Desktop.
Import module with standard name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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