view pyloader/require.py @ 87:29ca60f428cc

STUB: tests/test.py tests/test_include.txt
author Jeff Hammel <k0scist@gmail.com>
date Fri, 21 Mar 2014 22:27:51 -0700
parents b57de7c38a74
children
line wrap: on
line source

def require(url):
    """
    import a module from the web
    url should be like scheme://host.name/path/to/module.py
    """
    import imp
    import os
    import tempfile
    import urllib2
    contents = urllib2.urlopen(url).read()
    filename = url.rsplit('/', 1)[-1]
    module = filename.rsplit('.', 1)[-1]
    dest = os.path.join(tempfile.gettempdir(), filename)
    f = file(dest, 'w')
    f.write(contents)
    f.close()
    return imp.load_source(module, dest)

# TODO: make an equivalent method for a tarball