python - Import error "no module named": When importing dynamic class -
all,
i'm putting system needs dynamically load modules , create instances of class within.
my code is:
tokens = os.path.splitext("a.b.c") try: module_str = str(tokens[0]) task_class_str = str(tokens[1][1:]) module = __import__(module_str, fromlist=[task_class_str]) task_class = getattr(module, task_class_str) except exception, e: stdout.error("failed import task module", module=module_str, task_class=task_class_str, exception=e) continue try: task = task_class(task) except exception, e: stdout.error("failed create task", task=task, module=module, task_class=task_class, exception=e) continue
the error is:
[error]: 'failed create task' task_class=<class a.b.c @ 0x7fd3beb13e88> exception=importerror('no module named c',) task=<d.e instance @ 0x25700e0> module=<module 'a.b' '.../a/b/__init__.pyc'>
the definition of class c contained in a/b/__init__.py
. why trying import class module when logging shows class bound?
when creating task object with
task = task_class(task)
task not initialized. trying instead?:
task = task_class()
also said definition of c in "init.py". way code reads file "a.b.py" instead.
Comments
Post a Comment