circular imports
mutual imports
cyclic imports
software development
programming concepts

What happens when using mutual or circular cyclic imports?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Circular imports happen when two modules depend on each other during initialization. The result is usually not an infinite loop, but a partially initialized module that fails when code asks for names that have not been defined yet.

What the Import System Is Doing

Using Python as the clearest example, importing a module means more than reading a file name. The interpreter creates a module object, places it in sys.modules, and then executes the top-level code in that file.

That detail matters. If a.py imports b.py while a.py is still executing, then b.py sees a module object for a, but it may not contain the names that b.py expects yet.

A Typical Failure Case

Consider two modules that import each other's symbols directly.

python
1# a.py
2from b import helper_b
3
4def helper_a():
5    return "A"
6
7print(helper_b())
python
1# b.py
2from a import helper_a
3
4def helper_b():
5    return f"B calls {helper_a()}"

Running python a.py usually fails with an error like "cannot import name ... from partially initialized module". The failure happens because b.py tries to import helper_a before a.py has reached the function definition.

Why the Error Appears

The key problem is timing:

  1. Python begins loading a.
  2. a asks for b.
  3. Python begins loading b.
  4. b asks for helper_a from a.
  5. a exists only in partial form, so helper_a is not ready yet.

This is why circular imports are initialization problems, not only dependency-graph problems. Two modules can reference each other safely in some designs, but not if both references must be resolved at import time.

Common Ways to Break the Cycle

The cleanest fix is usually to move shared code into a third module.

python
# common.py
def helper_a():
    return "A"
python
1# a.py
2from b import helper_b
3
4print(helper_b())
python
1# b.py
2from common import helper_a
3
4def helper_b():
5    return f"B calls {helper_a()}"

Now the dependency graph is one-way rather than cyclic.

Another option is a local import when the dependency is needed only inside a function:

python
1# b.py
2def helper_b():
3    from a import helper_a
4    return f"B calls {helper_a()}"

This works because the import is delayed until runtime, after both modules have had a chance to finish initialization.

Imports for Types vs Imports for Behavior

Modern Python projects often hit circular imports only because of type hints. If the imported name is needed for annotations rather than runtime behavior, TYPE_CHECKING and postponed evaluation can help.

python
1from __future__ import annotations
2from typing import TYPE_CHECKING
3
4if TYPE_CHECKING:
5    from service import Service
6
7
8class Repository:
9    def attach(self, service: Service) -> None:
10        self.service = service

This keeps static typing intact without forcing the runtime import during module initialization.

Design Implications

Circular imports are usually a design smell. They often indicate that two modules know too much about each other or that a shared abstraction is missing.

That does not mean every cycle is catastrophic, but recurring circular-import fixes are a strong hint to reconsider module boundaries. Good refactors often involve:

  • moving shared constants or interfaces into a lower-level module
  • separating data models from business logic
  • using dependency injection instead of module-level singletons

Common Pitfalls

  • Assuming circular imports mean infinite recursion when the typical failure is partial initialization.
  • Sprinkling local imports everywhere instead of correcting the underlying module design.
  • Running heavy side effects at module top level, which makes import order much harder to reason about.
  • Forgetting that type hints can create runtime cycles unless they are guarded properly.
  • Fixing one circular dependency locally while leaving the same structural pattern in place elsewhere.

Summary

  • Circular imports usually produce partially initialized modules, not endless recursion.
  • The failure occurs when one module needs names from another before initialization has finished.
  • Moving shared code into a third module is often the best fix.
  • Local imports can break a cycle when the dependency is only needed at runtime.
  • Type-only imports should use tools such as TYPE_CHECKING to avoid unnecessary runtime coupling.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions