loading external paths and modules
Closed this issue · 1 comments
typemytype commented
less exec :)
and guessing with formatting an import str
import imp
def loadFilterFromPath(path, functionName):
"""
loadFilterFromPath("path/to/a/python/file/withAPen.py", "myFilterPen")
"""
f = open(path, "r")
try:
module = imp.load_source("ExternalPenBallWizard", path, f)
result = getattr(module, functionName)
except:
result = None
f.close()
return result
def loadFilterFromModule(module, functionName):
"""
loadFilterFromModule("robofab.pens.filterPen", "FlattenPen")
"""
try:
return __import__(module, fromlist=[functionName])
except:
return None
loicsander commented
Implemented, thanks, much cleaner!