You Are Here Home > Python ImportError: cannot import name X

Python ImportError: cannot import name X

This was strange, I had a bunch of classes in a file and was trying to import one of them from a file in a child folder.

The package looked like this:

/main_classes.py <– “import child” was in here way on top
/child/__init__.py <– For every file in this folder, import it
/child/some_file.py <– Import a class in main_classes.py *Error*

The reason was that I was doing “import child” way on top before implementing the class I was importing in some_file.py.

I moved the “import child” line from the top of the main_class.py to the constructor of the class I was implementing and it fixed the issue.

I hope this made sense :) (and I know it will to the person with this problem ;) )

Python ImportError: cannot import name X
Filed under: Python   Posted by: Codehead

Got a Question?

Get answers here.

8 Comments »

  1.  

    Thanks a lot! That was a very useful post :D
    And yes, it made sense :P

    Comment

  2.  

    yes, kewl post, mr. snake-charmer — solved my problem too :) now i can continue choking the snake.

    Comment

  3. luislope:
     

    and what happen if you hace a bouch of classes on:

    /ab/ab_classes.py

    and now make a package like this:

    /a/a_classes.py
    /b/b_classes.py

    ???

    Comment

  4. luislope:
     

    ok ok ok I got, niceeeee. thanks.

    Comment

  5. Daniel:
     

    Thx for the info – it also solved my problem – but i had to reread your post quite often to understand it.

    The problem is a **circular Reference** – as child gets imported in parent, which also wants to import parts of parent.

    Comment

  6. pointer:
     

    thanks a lot for the post. it does make sense to me.

    Comment

  7.  

    Thanks a lot, saved me a lot of searching :)

    Comment

  8. ysa:
     

    thanks so much for solving my problem.

    ***would take “years” to find it.heheheeh…
    saved me a lot of searching [2]!

    Comment

RSS feed for comments on this post. TrackBack URL

Leave a comment