Blog |

How to Fix a Circular Import in Python

How to Fix a Circular Import in Python
Table of Contents

Think of two pieces of code (let's call them A and B) that need each other to work. A says, "I need B to do my job," and at the same time, B says, "I need A to do my job." But if A is waiting for B to work, and B is waiting for A to work, neither can start working. This "waiting on each other" situation causes Python to get stuck and throw a circular import error.


Here’s a real life example of this error message:

importerror: cannot import name 'db' from partially initialized module 'app' (most likely due to a circular import)

This indicates that at some point in the code, there's an attempt to import an entity named db from a module named app.

What does the error actually mean?

The term partially initialized module 'app' means that when the import of db is attempted, the module app hasn't been fully loaded yet. Python needs to execute the entire body of a module to consider it fully initialized. If this process is interrupted or made conditional by a circular dependency, not all definitions within the module will have been executed, leading to this partial initialization problem.

The error hints at a circular import being the likely culprit. The phrase (most likely due to a circular import) means that app might be trying to use something from another module, which in turn, directly or indirectly, tries to import app again (or something from app). Python then finds itself stuck in a loop where neither import can complete first.

Since app hasn't finished initializing (it's waiting to complete the import before it can finalize its own setup), any attempt from the second module to access components of the first will encounter those components in an undefined or incomplete state.

Unfortunately, Python does not explicitly name the other module involved in the circular import with app. The error message generally points out the immediate issue—attempting to import a name from a partially initialized module—without detailing the chain of imports that led to the circular dependency. You’ll need to carefully review the traceback provided by Python when the error occurs. The traceback shows the sequence of import statements executed up to the point of the error, which can help you identify the circular dependency.

A simpler example

Here’s a simple example of an error thrown due to a circular dependency. Two python modules and are created to achieve this:

from test2 import Class2
class Class1:
    obj = Class2()

from test1 import Class1
class Class2:
    obj = Class1()

In the above example, the initialization of obj in test1 depends on test2, and obj in test2 depends on test1. This is a circular dependency since both files attempt to load each other. Therefore, running (or causes an ImportError: cannot import name error:

Traceback (most recent call last):
  File "", line 1, in 
    from test2 import Class2
  File "", line 1, in 
    from test1 import Class1
  File "", line 1, in 
    from test2 import Class2
ImportError: cannot import name 'Class2' from partially initialized module 'test2' (most likely due to a circular import) (

Strategies to resolve a circular import error

Breaking a circular dependency often involves reorganizing your imports or refactoring your code. Each of the strategies below has trade-offs, and the best solution often depends on the specific structure and requirements of your project.

  • Move the imported classes to a third file and import them from this file.
  • Move import statements into functions or methods where the imported entities are actually used. This delays the import until the function is called, which might be after all modules have been fully initialized.
  • Use Python’s built-in importlib library to import modules dynamically, which means you can import modules within the function or method where they are needed, rather than at the top of a file. This defers the import until it's absolutely necessary, thus avoiding the problems caused by the circular dependencies at module load time.

Track, analyze and manage errors with Rollbar

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Python errors easier than ever. Try it today!

Related Resources

"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

Error Monitoring

Start continuously improving your code today.

Get Started Shape