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: CodingRecipes

13 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

     
  9. AJ:
     

    You are putting the imports in your constructor? That seems a little weird (and extremely against the standards lol). Alternatively you could separate the parent code, and re-order the imports.

    For instance:
    /parent1.py/ <–first import parent2, then children
    /parent2.py/ <– part of parent code needed by children
    /children/childa.py
    /children/childb.py <– imports parent2
    /children/__init__.py <– same as before

    Comment

     
  10. Mary:
     

    Thank you, thank you, thank you, thank you!

    Comment

     
  11. Brian:
     

    I had this same issue using mod_python trying to import psycopg2. I had to move the

    import psycopg2

    under

    def handler(req):
    import psycopg2

    Comment

     
  12. John Villar:
     

    Can’t believe this was the error i was having. One million thanks to you pal!!! :-)

    Comment

     
  13.  

    Muy útil, muchas gracias.

    Comment

     

RSS feed for comments on this post. TrackBack URL

Leave a comment