Skip to main content

Python · 5 common causes

ImportError: circular import

Updated

Two modules import each other, so one is still half-executed when the other tries to read a name from it.

The exact message

Traceback (most recent call last):
  File "/app/main.py", line 1, in <module>
    from services import create_user
  File "/app/services.py", line 1, in <module>
    from models import User
  File "/app/models.py", line 1, in <module>
    from services import audit
ImportError: cannot import name 'User' from partially initialized module 'models' (most likely due to a circular import)

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is ImportError: circular import.

How to fix it

Cause 1 of 5

A genuine cycle: A imports B at module level and B imports A at module level.

Move one of the imports inside the function that needs it. By the time that function runs, both modules are fully loaded, so the cycle disappears. It is a small change and a legitimate pattern, not a hack.

Cause 2 of 5

The import exists only for type annotations.

Put it under if TYPE_CHECKING: and quote the annotation, or add from __future__ import annotations so all annotations are strings. Type checkers still resolve it; the interpreter never performs the import.

Cause 3 of 5

Shared code is living in the wrong module.

Extract what both modules need into a third module that neither imports back from. This is usually the real fix — it removes the cycle instead of arranging the code so the cycle happens to work.

Cause 4 of 5

The from x import y form requires the name to exist at import time.

import models followed by models.User at the point of use binds late and often breaks the cycle by itself, because the module object exists before its contents are finished.

Cause 5 of 5

A local file shadows the module being imported.

A file named email.py, types.py or the same as your own package makes the import resolve back to itself. Rename it, and delete the stale __pycache__ alongside it.

Stopping it coming back

Keep dependencies pointing one way — models to nothing, services to models, views to services — and let an import-linter rule fail the build when a layer imports upwards.

ImportError: circular import — common questions

Why does the traceback show the import chain twice?
Because it is showing the path into the cycle. Read it from the top: each frame is the module that triggered the next import, and the last one is the module that was still initialising when the name was requested.
Are function-level imports bad practice?
They cost a dictionary lookup per call, which is negligible, and they are explicitly endorsed for breaking cycles and for deferring expensive imports. Overusing them hides dependencies, so prefer restructuring when the cycle is a design smell.
Why does it work in one entry point but not another?
Because the module that gets imported first differs, and that decides which module is mid-initialisation when the cycle closes. That fragility is the reason to remove the cycle rather than find the ordering that happens to survive.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor