Skip to content

Instantly share code, notes, and snippets.

@stamat
Last active November 15, 2017 12:32
Show Gist options
  • Save stamat/5895822 to your computer and use it in GitHub Desktop.
Save stamat/5895822 to your computer and use it in GitHub Desktop.
Dynamically imports python modules by given module URI
import os
import imp
def importFromURI(self, uri, absl=False):
if not absl:
uri = os.path.normpath(os.path.join(os.path.dirname(__file__), uri))
path, fname = os.path.split(uri)
mname, ext = os.path.splitext(fname)
no_ext = os.path.join(path, mname)
if os.path.exists(no_ext + '.pyc'):
try:
return imp.load_compiled(mname, no_ext + '.pyc')
except:
pass
if os.path.exists(no_ext + '.py'):
try:
return imp.load_source(mname, no_ext + '.py')
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment