Possible Duplicate:
Python: Circular (or cyclic) imports
I’m new to Python, and I’m having an issue, but I’m not exactly sure if this is my issue. I have two files, user.py and comments.py. In user.py, I do
from comments import Comment
and in comments.py I do
from user import User
My user loads fine, but when I load the URL that leads to comments, I get a server error. Commenting out the from comments import Comment fixes the problem. Am I doing something wrong?
Yes, you have a circular import, and those cause many many problems. If you think about what is actually happening when you import, it is analogous to saying, “copy the code from file x in to this file,” but if you copy from x to y then back from y to x, you’ve created an infinite loop where it’s difficult to impossible for the interpreter to figure out which module is supposed to supersede or load which in which situations. However, if your program is architected properly, you should not have any. What reason do you have for having this circular import? Chances are you don’t actually need it at all if we look at the problem a little more carefully.