2012-11-14 60 views
1

我想动态加载给定字符串中的类。但是,我不知道这个类将会在哪个文件中,所以我将不得不搜索所有文件。我试过这个,但我得到AttributeError: 'module' object has no attribute 'MyClass'即使我100%肯定该模块(当前迭代)具有类:在Python中动态导入类

target = 'MyClass' 
module_names = [mf[0:-3] for mf in os.listdir('application/models') if mf.endswith(".py")] 
modules = [imp.new_module(x) for x in module_names] 
for module in modules: 
    try: 
     target_class = getattr(module, target) 
    except ImportError, AttributeError: 
     continue 

if target_class: 
    print 'found class' 

看来我得到非常接近。我想要的不是将搜索限制在一个文件夹中,而是可能是多个文件夹。我的代码有什么问题?

编辑:好,现在我想这样的事情,但仍然得到了同样的错误:

for m in module_names: 
     try: 
      x = reload(__import__(m)) 
      target_class = getattr(x, target) 
     except ImportError, AttributeError: 
      continue 
     else: 
      break 

    if target_class: 
     print 'found class' 
+0

你可能需要一个'break'一旦你找到'target_class' – mgilson

+0

检查更新 – iabdalkader

回答

2

从文档上imp.new_module,返回模块。这意味着它永远不会包含你的班级。

也许你想要做的是将你的目标目录添加到sys.path并使用__import__动态导入这些模块,然后检查你的课程?


下面的代码工作对我来说:

modules = ['foo','bar'] 
for mod in modules: 
    try: 
     x = reload(__import__(mod)) 
    except ImportError: 
     print "bargh! import error!" 
     continue 
    try: 
     cls = getattr(x,'qux') 
    except AttributeError: 
     continue 

a = cls() 
print a.__class__.__name__ 

foo.pybar.py是在同一个目录:

#foo.py 
class foo(object): 
    pass 

和:

#bar.py 
class qux(object): 
    pass 
+0

请看编辑 – Snowman

+0

你是否得到'ImportError'或'AttributeError'? – mgilson

+0

'AttributeError:'module'object has no attribute'MyClass'' – Snowman

1

按照documentationnew_module回报和模块:

imp.new_module(name)
Return a new empty module object called name. This object is not inserted in sys.modules.

你可能想要看看imp.load_source。这只是一个简单的例子:

class Test: 
    pass 

而且

In [19]: m = imp.load_source("test", "./test.py") 
In [20]: getattr(m, "Test") 
Out[20]: <class test.Test at 0x1fe6120> 
0

继EXA mple在imp documentation:在同一个目录

文件名为hello.py:

def myFunction(): 
     return "Hello World!" 

进口你好动态(W/O尝试终于除外):

fp, pathname, description = imp.find_module("hello") 
hello = imp.load_module("hello", fp, pathname, description) 
hello.myFunction() # returns 'Hello World!'