views:

988

answers:

1

I have two files, node.py and path.py, which define two classes, Node and Path, respectively.

Up to today, the definition for Path referenced the Node object, and therefore I had done

from node.py import *

in the path.py file.

However, as of today I created a new method for Node that references the Path object.

I had problems when trying to import path.py: I tried it, and when the program ran and called the Path method that uses Node, an exception rose about Node not being defined.

What do I do?

+4  A: 

This is a great article that explains circular imports in python http://effbot.org/zone/import-confusion.htm

The easiest way to fix this is to move the path import to the end of the node module.

Nadia Alramli
Okay, but the thing is, I have two other modules `tree.py` and `block.py` in that package that require `node.py` and are required by `path.py`. So am I supposed to put them all in one file?I liked having one module per class.
cool-RR
Have you tried my suggestion? It'll probably work. Just move the import to the end of the file. I advice you to read the article to understand why this is happening.
Nadia Alramli
You're right Nadia. I misread your suggestion at first. It worked.
cool-RR
By the way, is there some reason other than you "liking" it that you want one class per module? The few times I've seen this preference, it was because it's Java-like.
ΤΖΩΤΖΙΟΥ