Python Glossary Русский

Python 3.15+Exceptionsexception

ImportCycleError

built-in exceptions

A subclass of ImportError: a lazy import, on first use, found that the module imports itself directly or through a chain.

Syntax

class ImportCycleError(ImportError)

A regular import with a cycle often “half works”: the second module receives a partially loaded first one and fails with AttributeError or an ImportError about a “partially initialized module”. A lazy import postpones loading until the name is first used, so the cycle shows up later — at the point of use, and that is when ImportCycleError is raised. Since it subclasses ImportError, existing code with except ImportError catches it. The cure is the same as for any import cycle: move the shared part into a third module or import inside a function.

Examples

print(issubclass(ImportCycleError, ImportError))  # → True
print(ImportCycleError.__mro__[1].__name__)  # → ImportError

See also

Open in the full glossarydocs.python.orgReport an inaccuracy