Skip to content

Instantly share code, notes, and snippets.

@duquesnay
Last active November 17, 2017 17:51
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 duquesnay/1979518a6508e422d6912c1d84302799 to your computer and use it in GitHub Desktop.
Save duquesnay/1979518a6508e422d6912c1d84302799 to your computer and use it in GitHub Desktop.
Dynamic build module from URL
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
@duquesnay
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment