ImportError: cannot import name from partially initialized module (most likely due to a circular import)
Updated
The Python error "cannot import name … from partially initialized module" means two modules import each other: a circular import. Four ways to break the loop.
What you see
from customers import Customer
class Order:
def __init__(self, customer: Customer):
self.customer = customerfrom orders import Order
class Customer:
def new_order(self) -> Order:
return Order(self)import ordersTraceback (most recent call last):
File "/home/me/project/main.py", line 1, in <module>
import orders
File "/home/me/project/orders.py", line 1, in <module>
from customers import Customer
File "/home/me/project/customers.py", line 1, in <module>
from orders import Order
ImportError: cannot import name 'Order' from 'orders' (consider renaming '/home/me/project/orders.py' if it has the same name as a library you intended to import)What it means
Python runs a module from top to bottom the first time it is imported. Here orders starts running, and its first line imports customers. customers, in turn, asks for Order from orders, but orders has only got as far as its first line: class Order has not run yet. That half-run module is what "partially initialized" means.
Python 3.13 and later word the same failure differently, and less helpfully: they drop "partially initialized" and suggest renaming orders.py in case it shadows a library. Unless your file really has a library's name, the cause is still the loop.
The cycle is the problem, not either file on its own. Each import is fine; together they wait for each other.
Why it happens
- Two modules that need each other's names at import time, as in the example: models that refer to each other, or a helper module that imports the module that uses it.
- Imports added only for type hints that create a loop the code itself does not need.
- A file of yours named like a module it imports, such as
random.pydoingimport random. That is a loop of one file with itself; it has its own page.
How to fix it
Move what both need into a third module. If orders and customers both need a base class or constants, put them in models.py and import from there. This is the fix that removes the loop.
Import the module, not the name, and look the name up when the code runs:
import orders
class Customer:
def new_order(self):
return orders.Order(self)
Import inside the function that needs it, so the import runs at call time, after both modules have loaded:
class Customer:
def new_order(self):
from orders import Order
return Order(self)
For imports used only in type hints, guard them with TYPE_CHECKING, which is false when the program runs:
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from orders import Order
How each Python version words it
| Python | Prints |
|---|---|
| 3.9–3.12 | ImportError: cannot import name 'Order' from partially initialized module 'orders' (most likely due to a circular import) (/home/me/project/orders.py) |
| 3.13–3.15 | ImportError: cannot import name 'Order' from 'orders' (consider renaming '/home/me/project/orders.py' if it has the same name as a library you intended to import) |
Run on CPython 3.9.6, 3.10.22, 3.11.17, 3.12.15, 3.13.17, 3.14.9 and 3.15.1, on October 25, 2026.
3.9 to 3.12 name the cause: "from partially initialized module 'orders' (most likely due to a circular import)". 3.13 and later print "cannot import name 'Order' from 'orders'" with a hint to rename the file, the hint added for files that shadow a library.
Python 3.14 evaluates annotations lazily, but the from orders import Order line still runs at import time, so the loop fails there just the same.
Questions
Why did it work until I added one import?
The new import closed the loop. Python can load cycles where nothing needs a name before it is defined; a from x import y at the top demands y immediately.
Python 3.13 tells me to rename my file. Should I?
Only if the file has the name of a module you also install or import from the standard library. For orders.py and customers.py the hint is a false lead: break the loop instead.
Which import should I remove?
Look at what each module uses from the other. The one that uses less can usually import inside a function, or both can import from a new shared module.
Sources
- The import system, Python language reference
- Programming FAQ: what are the best practices for using import?, Python documentation
typing.TYPE_CHECKING, Python documentation
Read on October 25, 2026.