Last active
November 17, 2017 17:51
-
-
Save duquesnay/1979518a6508e422d6912c1d84302799 to your computer and use it in GitHub Desktop.
Dynamic build module from URL
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
def load_module(url): | |
import urllib.request | |
from os.path import splitext, basename | |
from types import ModuleType | |
u = urllib.request.urlopen(url) | |
sourcename = url.split('/')[-1] | |
modulename = splitext(basename(sourcename))[0] | |
sourcecode = u.read().decode('utf-8') | |
code = compile(sourcecode, url, 'exec') | |
mod = ModuleType(modulename) | |
mod.__file__ = url | |
exec(code, mod.__dict__) | |
return mod |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I know I know, dangerous n stuff. But having to import python code in a third service I better have the code in public git so I can unit test and version it elsewhere. That's my production code I guess