2015-09-04 199 views
0

在Python中,當我使用此導入語句breze.learn.mlp導入iter_minibatches時,出現以下錯誤。導入錯誤python

這裏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 
+4

*「這裏'iter_minibatches'是一個在mlp.py中定義的函數」* - 你能證明它嗎? – Sam

+0

'dir(breze.learn.mlp)'輸出了什麼? – IanAuld

+0

def iter_minibatches(lst,batch_size,dims,n_cycles = False,random_state = None): print'inside iter mini batches:',lst [0] 批次= [minibatches(i,batch_size,d)for i,d in zip(lst,dims)] .... –

回答

4

您有一個循環導入; mlp進口basemlp進口:

# 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之後將還沒有被執行,從而導入這些線限定將失敗的任何對象。

避免循環導入,或者如果您必須擁有它們,請延遲導入其中一個模塊,以確保在另一個模塊中定義您需要的對象。

+0

趕上@MartijnPieters! – Sam

+0

我正在使用** from breze.learn.mlp在base.py中導入iter_minibatches **並從breze.learn.base導入**導入** SupervisedModel **用於mlp.py ... 您能否建議我如何解決它....我是新手到python –

+0

@MartijnPieters我只是在從breze.learn.mlp添加iter_minibatches後base.py –