Skip to content

Instantly share code, notes, and snippets.

@xmodar
Last active August 31, 2021 23:44
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 xmodar/21c4abb7c754cc22bd7c6dfc6361aebf to your computer and use it in GitHub Desktop.
Save xmodar/21c4abb7c754cc22bd7c6dfc6361aebf to your computer and use it in GitHub Desktop.
import tempfile
import urllib.request
import importlib.util
from pathlib import Path
def import_from_url(url):
"""Import a module from a given URL"""
with tempfile.TemporaryDirectory() as path:
path = Path(path) / Path(url).name
urllib.request.urlretrieve(url, path)
spec = importlib.util.spec_from_file_location(path.stem, path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment