# Python Circular Imports: Why They Happen and How to Fix Them

DevFeed: [Python Circular Imports: Why They Happen and How to Fix Them](<https://devfeed.tech/articles/python-circular-imports-why-they-happen-and-how-to-fix-them-102215.md>)

Original publisher: [Read original article](<https://realpython.com/python-circular-imports/>)

Author: Srujana Maddula

Published: 2026-10-27T14:00:00Z

Content type: tutorial

Language: en

Sources: [Real Python](<https://devfeed.tech/sources/real-python.md>)

Topics: [Python](<https://devfeed.tech/topics/python.md>), [Learning](<https://devfeed.tech/topics/learning.md>), [import](<https://devfeed.tech/topics/import.md>)

Tags: [errors](<https://devfeed.tech/tags/errors.md>), [examples](<https://devfeed.tech/tags/examples.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [import](<https://devfeed.tech/tags/import.md>), [python](<https://devfeed.tech/tags/python.md>)

## AI overview

Python adds a module to `sys.modules` before executing it, so a circular import can expose a partially initialized module. The error occurs when code requests a name that has not yet been defined; importing a name directly can raise `ImportError`, while accessing it through a module can raise `AttributeError`. The reliable fix is to restructure dependencies so they flow in one direction. For type-only references, use a `TYPE_CHECKING` guard, with string annotations needed on Python 3.13 and earlier. If restructuring is not practical, importing inside the function that needs the dependency works regardless of import order. Moving imports to the bottom or importing the module object can also work in some cases, but these approaches depend on import order; bottom-of-file imports also violate PEP 8 and trigger linter warnings.

## Source excerpt

Confused by Python circular imports? Learn why they happen, how Python's import system works, and how to fix import errors using practical examples.