Skip to content

Instantly share code, notes, and snippets.

@OhadRubin
Forked from koji-kojiro/import_from_gist.py
Last active December 17, 2020 13:09
Show Gist options
  • Save OhadRubin/a541b29e641a5eb76ec626ab49417a1a to your computer and use it in GitHub Desktop.
Save OhadRubin/a541b29e641a5eb76ec626ab49417a1a to your computer and use it in GitHub Desktop.
[Python] import from Gist (works on python3)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def load_gist(gist_id):
"""translate Gist ID to URL"""
from json import load
from urllib.request import Request, urlopen
gist_api = urlopen("https://api.github.com/gists/" + gist_id)
files = load(gist_api)["files"]
files_head_member = list(files.keys())[0]
raw_url = files[files_head_member]["raw_url"]
gist_src = urlopen(raw_url).read()
return gist_src
def import_from_gist(gist_id):
"""import from Gist"""
from sys import path
from tempfile import mkdtemp
gist_src = load_gist(gist_id)
temp_dir = mkdtemp()
path.append(temp_dir)
with open(temp_dir + "/module{}.py".format(gist_id), "w") as f:
# print(gist_src)
f.write(gist_src.decode())
exec("import module{} as mod".format(gist_id),globals())
return mod
if __name__ == "__main__":
gist_id = "55b8bec6e652c860f287288d98bc507f"
mod = import_from_gist(gist_id)
mod.hello()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment