在Python中,当我使用这个导入语句breze.learn.mlp import iter_minibatches时,会出现以下错误。
此处iter_minibatches
是mlp.py中定义的函数。
Traceback (most recent call last):
File "/home/vinod/PycharmProjects/MLPonTheano/MLPbreze.py", line 15, in <module>
from breze.learn.mlp import Mlp, FastDropoutNetwork
File "/home/vinod/breze/breze/learn/mlp.py", line 22, in <module>
from breze.learn.base import SupervisedModel
File "/home/vinod/breze/breze/learn/base.py", line 21, in <module>
from breze.learn.mlp import iter_minibatches
ImportError: cannot import name iter_minibatches
答案 0 :(得分:4)
你有一个循环导入; mlp
导入base
导入mlp
:
# executing mlp.py
File "/home/vinod/breze/breze/learn/mlp.py", line 22, in <module>
from breze.learn.base import SupervisedModel
# executing base.py
File "/home/vinod/breze/breze/learn/base.py", line 21, in <module>
# this tries to import from mlp again, but mlp isn't done yet
from breze.learn.mlp import iter_minibatches
from breze.learn.base import SupervisedModel
之后的任何行都不会被执行,因此导入这些行定义的任何对象都将失败。
避免循环导入,或者如果必须使用循环导入,则延迟导入其中一个模块以确保定义另一个模块中所需的对象。