Skip to content

circular dependency

A circular dependency is a relationship among two or more components in which each one depends on another, directly or indirectly, so that following the chain of dependencies eventually loops back to where it started. The components can be modules, classes, packages, or whole services, and their references form a cycle in the dependency graph rather than a one-way, acyclic chain:

In a circular dependency Module A to B to C loops back to A, unlike the one-way A to B to C acyclic chain.
A Cycle Loops Back Where an Acyclic Chain Does Not

Because no member of the cycle can be built, loaded, or understood on its own, a circular dependency tightens the coupling between the parts and makes each one harder to test or reuse in isolation. A change to one member can ripple around the loop and back, and an ordering that initializes every component in turn may not exist.

The cycle causes trouble at cleanup time too. A reference-counting garbage collector can’t reclaim objects that point at each other, because their counts stay above zero even after nothing else refers to them.

In Python, the pattern often surfaces as a circular import, where one module imports a second module that imports the first. When the second module reads a name from the first before the first has finished running, Python raises an ImportError or AttributeError from the partially initialized module. Common ways to break such a cycle include:

  • Extract shared code: Move whatever both parts need into a separate module that each one depends on.
  • Invert a dependency: Introduce an abstraction so the concrete parts depend on it rather than on each other.
  • Merge the parts: Combine two tightly bound components into one when splitting them adds no value.

Static-analysis tools surface these cycles by scanning the dependency graph, and a design that keeps that graph acyclic sidesteps the problem from the start.

Python import: Advanced Techniques and Tips

Tutorial

Python import: Advanced Techniques and Tips

The Python import system is as powerful as it is useful. In this in-depth tutorial, you'll learn how to harness this power to improve the structure and maintainability of your code.

intermediate python stdlib

For additional information on related topics, take a look at the following resources:


By Martin Breuss • Updated July 26, 2026