Created
December 21, 2012 17:01
-
-
Save tsileo/4354068 to your computer and use it in GitHub Desktop.
how to instantiate a python class from a string using imp.
This file contains hidden or 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 imp | |
def load_class(full_class_string): | |
""" | |
Dynamically load a class from a string | |
>>> klass = load_class("module.submodule.ClassName") | |
>>> klass2 = load_class("myfile.Class2") | |
""" | |
class_data = full_class_string.split(".") | |
module_str = class_data[0] | |
class_str = class_data[-1] | |
submodules_list = [] | |
if len(class_data) > 2: | |
submodules_list = class_data[1:-1] | |
f, filename, description = imp.find_module(module_str) | |
module = imp.load_module(module_str, f, filename, description) | |
# Find each submodule | |
for smod in submodules_list: | |
path = os.path.dirname(filename) if os.path.isfile(filename) else filename | |
f, filename, description = imp.find_module(smod, [path]) | |
# Now we can load the module | |
try: | |
module = imp.load_module(" ".join(class_data[:-1]), f, filename, description) | |
finally: | |
if f: | |
f.close() | |
# Finally, we retrieve the Class | |
return getattr(module, class_str) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment