10

I have a directory structure:

network/__init__.py
network/model.py
network/transformer/__init__.py
network/transformer/t_model.py

both __init__.py files have appropriate

__all__ = [
    "model",  # or "t_model" in the case of transformer
    "view",
    ]

In t_model.py, I have

from .. import model

but it says:

ImportError: cannot import name model

If I try

from ..model import Node

it says:

ImportError: cannot import name Node

These are very confusing errors.


Edit: Even an absolute import fails:

import network as N
print(dir(N), N.__all__)
import network.model as M

['__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'transformer'] ['model', 'view']
Traceback (most recent call last):..........
AttributeError: 'module' object has no attribute 'model'

Edit: It was a circular import.

Neil G
  • 32,138
  • 39
  • 156
  • 257

2 Answers2

4

This works for me. Can you run/import model.py? If it has syntax errors you can't import it. (In general I recommend not to do relative imports, the use of them is limited).

Your absolute import is very confusing. The way to do an absolute import in this package is:

from network model import Node

This works fine.

I have a program.py in the top level (above network):

from network.transformer import t_model

And the t_model.py looks like this:

from .. import model
print "Model", model

from ..model import Node
print "Node", Node

from network.model import Node
print "Absolute", Node

And the output is:

Model <module 'network.model' from '/tmp/network/model.pyc'>
Node <class 'network.model.Node'>
Absolute <class 'network.model.Node'>

So as you can see it works fine your error is somewhere else.

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
  • If I do from network.model import Node, it says ImportError: cannot import name Node – Neil G May 26 '11 at 22:57
  • @Neil G: Of course. As I showed above above your relative imports are correct, the error is somewhere else, not in how you write the import. Now if you read my answer, you'll see that I ask you a question. If you want help with this I suggest you answer it. – Lennart Regebro May 27 '11 at 05:13
  • 1
    Importing model.py was the problem. It turned out that model.py was importing t_model.py, which imported model.py. I was almost pulling my hair out on this one. Thank you very much for your help. – Neil G May 27 '11 at 06:29
  • Also, is it true that I should prefer absolute imports to relative ones? Or vice versa? – Neil G May 27 '11 at 06:30
  • @Neil G: Super! Glad to help! In my experience you almost never need relative imports, and absolute imports cause less confusion. :-) – Lennart Regebro May 27 '11 at 06:51
0

From this question.

project/
    program.py           # entry point to the program
    network/         
        __init__.py
        transform/       # .. will target network
            __init__.py

I think you can also execute network/model.py from the directory below and get relative imports to network. so...

network/
    model.py
    __init__.py

then you would start the program with $ python network/model.py. You may or may not need to hit __init__.py instead. I had an app engine program that targeted module/__init__.py and relative imports worked great.

Community
  • 1
  • 1
Matt
  • 1,222
  • 1
  • 9
  • 18