Skip to content

Instantly share code, notes, and snippets.

@colobas
Created May 24, 2019 00:24
Show Gist options
  • Save colobas/caa512e123792271449b3bf9887b4539 to your computer and use it in GitHub Desktop.
Save colobas/caa512e123792271449b3bf9887b4539 to your computer and use it in GitHub Desktop.
util to import remote modules. can be used with gists, for example.
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