Created
May 24, 2019 00:24
-
-
Save colobas/caa512e123792271449b3bf9887b4539 to your computer and use it in GitHub Desktop.
util to import remote modules. can be used with gists, for example.
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 tempfile | |
import requests | |
from importlib.machinery import SourceFileLoader | |
def import_remote_module(module_name, url): | |
""" | |
import a file from a URL, given by `url` | |
--- | |
returns the imported module | |
--- | |
example usage: | |
>>> mymodule = import_remote_module("mymodule", "https://the.url/where/the/file/lives.py") | |
>>> mymodule.some_function_on_the_module() | |
""" | |
with tempfile.NamedTemporaryFile() as f: | |
f.write(requests.get(url).content) | |
f.seek(0) | |
return SourceFileLoader(module_name, f.name).load_module() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment