Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Circular dependency in Python

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?

like image 689
Ram Rachum Avatar asked May 21 '09 20:05

Ram Rachum


People also ask

What is a circular dependency python?

In python, a module can be made by importing other modules. In some cases, a Circular dependency is created. Circular dependency is the case when some modules depend on each other. It can create problems in the script such as tight coupling, and potential failure.

What is Circle dependency?

In software engineering, a circular dependency is a relation between two or more modules which either directly or indirectly depend on each other to function properly. Such modules are also known as mutually recursive.


1 Answers

Importing Python Modules is a great article that explains circular imports in Python.

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

like image 85
Nadia Alramli Avatar answered Sep 19 '22 09:09

Nadia Alramli