0

In Python, when I use this import statement breze.learn.mlp import iter_minibatches, am getting the following errors.

Here iter_minibatches is a function defined in 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
6
  • 4
    "Here iter_minibatches is a function defined in mlp.py" -- can you prove it? Commented Sep 4, 2015 at 14:38
  • What does dir(breze.learn.mlp) output? Commented Sep 4, 2015 at 14:39
  • def iter_minibatches(lst, batch_size, dims, n_cycles=False, random_state=None): print 'inside iter mini batches:', lst[0] batches = [minibatches(i, batch_size, d) for i, d in zip(lst, dims)] .... Commented Sep 4, 2015 at 14:39
  • breze.learn.mlp is a seperate file where it contains classes and seperate functions Commented Sep 4, 2015 at 14:40
  • @Sam: note the circular import. Commented Sep 4, 2015 at 14:40

1 Answer 1

4

You have a circular import; mlp imports base imports 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

Any line after the from breze.learn.base import SupervisedModel will not yet have been executed so importing any object defined by those lines will fail.

Avoid circular imports, or if you must have them, delay importing in one of the modules to make sure the objects you need in the other are defined.

Sign up to request clarification or add additional context in comments.

7 Comments

Good catch @MartijnPieters!
I am using from breze.learn.mlp import iter_minibatches in base.py and this import from breze.learn.base import SupervisedModel is used in mlp.py... Can you suggest me how to solve it.... I am newbie to python
@MartijnPieters I am facing this problem only after adding from breze.learn.mlp import iter_minibatches in base.py
@Vinodprime: yes, and that caused the circular import. Do you have to have that import there? Can you reorganise your objects to not have mlp import something from base again?
@MartijnPieters I have to use iter_minibatches in base.py, so I don't know wat to do
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.