How to debug Python import failure

18,794

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.

Share:
18,794
Neil G
Author by

Neil G

Interested in machine learning and Python.

Updated on August 23, 2022

Comments

  • Neil G
    Neil G almost 2 years

    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
    Neil G about 13 years
    Thanks. I have __init__.py in every folder.
  • Neil G
    Neil G about 13 years
    If I do from network.model import Node, it says ImportError: cannot import name Node
  • Lennart Regebro
    Lennart Regebro about 13 years
    @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.
  • Neil G
    Neil G about 13 years
    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
    Neil G about 13 years
    Also, is it true that I should prefer absolute imports to relative ones? Or vice versa?
  • Lennart Regebro
    Lennart Regebro about 13 years
    @Neil G: Super! Glad to help! In my experience you almost never need relative imports, and absolute imports cause less confusion. :-)