Last active
August 31, 2021 23:44
-
-
Save xmodar/21c4abb7c754cc22bd7c6dfc6361aebf to your computer and use it in GitHub Desktop.
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 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