circular dependency is a relation between two or more modules which either directly or indirectly depend on each other to function properly.
Questions tagged [circular-dependency]
1693 questions
735
votes
17 answers
What can I do about "ImportError: Cannot import name X" or "AttributeError: ... (most likely due to a circular import)"?
I have some code spread across multiple files that try to import from each other, as follows:
main.py:
from entity import Ent
entity.py:
from physics import Physics
class Ent:
...
physics.py:
from entity import Ent
class Physics:
...
I…

jsells
- 7,359
- 3
- 14
- 3
546
votes
16 answers
What happens when using mutual or circular (cyclic) imports?
In Python, what happens when two modules attempt to import each other? More generally, what happens if multiple modules attempt to import in a cycle?
See also What can I do about "ImportError: Cannot import name X" or "AttributeError: ... (most…

Xolve
- 22,298
- 21
- 77
- 125
473
votes
12 answers
Resolve build errors due to circular dependency amongst classes
I often find myself in a situation where I am facing multiple compilation/linker errors in a C++ project due to some bad design decisions (made by someone else :) ) which lead to circular dependencies between C++ classes in different header files…

Sandeep Datta
- 28,607
- 15
- 70
- 90
226
votes
16 answers
How to deal with cyclic dependencies in Node.js
I've been working with nodejs lately and still getting to grips with the module system, so apologies if this is an obvious question. I want code roughly like the below:
a.js (the main file run with node)
var ClassB = require("./b");
var ClassA =…

Runcible
- 3,008
- 3
- 19
- 19
196
votes
3 answers
How to avoid circular imports in Python?
I know the issue of circular imports in python has come up many times before and I have read these discussions. The comment that is made repeatedly in these discussions is that a circular import is a sign of a bad design and the code should be…

BWW
- 2,071
- 2
- 13
- 5
152
votes
8 answers
Why do circular imports seemingly work further up in the call stack but then raise an ImportError further down?
I'm getting this error
Traceback (most recent call last):
File "/Users/alex/dev/runswift/utils/sim2014/simulator.py", line 3, in
from world import World
File "/Users/alex/dev/runswift/utils/sim2014/world.py", line 2, in
…

CpILL
- 6,169
- 5
- 38
- 37
116
votes
17 answers
ImportError: cannot import name '...' from partially initialized module '...' (most likely due to a circular import)
I'm upgrading an application from Django 1.11.25 (Python 2.6) to Django 3.1.3 (Python 3.8.5) and, when I run manage.py makemigrations, I receive this messasge:
File "/home/eduardo/projdevs/upgrade-intra/corporate/models/section.py", line 9, in…

3WZ
- 1,165
- 2
- 7
- 5
95
votes
7 answers
Circular import dependency in Python
Let's say I have the following directory structure:
a\
__init__.py
b\
__init__.py
c\
__init__.py
c_file.py
d\
__init__.py
d_file.py
In the a package's __init__.py, the…

Ram Rachum
- 84,019
- 84
- 236
- 374
93
votes
5 answers
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…

Ram Rachum
- 84,019
- 84
- 236
- 374
59
votes
18 answers
Pros & Cons of putting all code in Header files in C++?
You can structure a C++ program so that (almost) all the code resides in Header files. It essentially looks like a C# or Java program. However, you do need at least one .cpp file to pull in all the header files when compiling. Now I know some people…

user15071
- 3,391
- 8
- 31
- 31
54
votes
11 answers
Cannot add reference to project because of a circular dependency error
I created 2 dummy projects in my application and named them BAL and DAL. When I build them, they build successfully. If I add a reference to BAL to the DAL project, it added nicely. But while adding the DAL reference to the BAL project, I get the…

Venkat
- 551
- 1
- 4
- 3
51
votes
3 answers
Dependency diagram in Dart/Flutter?
Is there a way to see how libraries in my Flutter package depend on each other?
Under libraries, I mean internal libraries, dart files under 'lib'.
Also, it would be great to check for circular dependencies between the libraries.

polina-c
- 6,245
- 5
- 25
- 36
47
votes
7 answers
Circular dependency in Django Rest Framework serializers
I'm fighting with circular dependencies within serializers in my web API written using Django Rest Framework 3. Whereas I know that circular dependencies in a project is almost always a sign of bad design, I can't find a decent way of avoiding it…

Mateusz Papiernik
- 840
- 1
- 7
- 12
45
votes
5 answers
Circular dependency in Java constructors
I have the following classes.
public class B
{
public A a;
public B()
{
a= new A();
System.out.println("Creating B");
}
}
and
public class A
{
public B b;
public A()
{
b = new B();
…

athena
- 5,579
- 8
- 30
- 31
45
votes
4 answers
Any good advice about how to avoid import cycle in Go?
I'm working on a Go project for a month. The good thing is Go is really highly efficient. But after a month of development I've already got thousands lines of code and many packages. To avoid import cycle is a major issue for me that anytime I got a…

Shane Hou
- 4,808
- 9
- 35
- 50