def load_from_file(filepath):
    class_inst = None
expected_class = 'MyClass'
mod_name, file_ext = os.path.splitext(os.path.split(filepath)[-1])
if file_ext.lower() == '.py':
    py_mod = imp.load_source(mod_name, filepath)
elif file_ext.lower() == '.pyc':
    py_mod = imp.load_compiled(mod_name, filepath)
if hasattr(py_mod, expected_class):
    class_inst = getattr(py_mod, expected_class)()
return class_inst
Dynamic module import in Python