Python Circular Import Error Solved

Python circular import is an error that occurs when two or more modules mutually depending on each other try to import before fully loading. Here’s how to solve it.

Written by Stephen Adesina
Published on Mar. 04, 2024
Python Circular Import Error Solved
Image: Shutterstock / Built In
Brand Studio Logo

A Python circular import can occur when two or more modules mutually depend on each other. This can happen if each module tries to import the other before fully loading, leading to unpredictable behavior and runtime errors

What Is Python Circular Import? 

Python circular import is an error that occurs when two modules mutually dependent on each other try to import before fully loading. It causes a loop in which each function tries to load the other and fails, creating unpredictable behavior and runtime errors. 

To avoid these issues, it’s essential to carefully consider the import statements in your code and use one of the methods described in the next section to break the cycle of imports. Below is an example of a Python circular import error statement.

"ImportError: cannot import name 'users_bp' from partially initialized module 'src.users.routes' (most likely due to a circular import) "

 

5 Ways to Avoid a Python Circular Import

If you encounter a Python circular import error in your code, there are a few methods you can try to eliminate it. Let’s take a look at each one below:

 

1. Import the Module Inside a Function

One way to avoid circular imports is to import the module inside a function, rather than at the top level of the module. This allows the module to be imported only when it’s needed, rather than when the module is first imported. For example:

def foo():
    from module1 import bar
    bar()

More on PythonFix ModuleNotFoundError: No Module Named ‘Sklearn‘

 

2. Use ‘Import As’ Syntax 

Another way to avoid circular imports is to use the import as syntax. This allows you to import the module using a different name, which can then be used to reference the module within your code. For example:

import module1 as m1

def foo():
    m1.bar()

 

3. Move the Import to the End of the Module 

A third option is to move the import statement to the end of the module after all the other code has been defined. This ensures that the module has been fully defined before another module imports it. For example:

def foo():
    pass

def bar():
    pass

import module1

 

4. Use the ‘ImportLib’ Library

Another option for escaping circular imports is to use the importlib library, which allows you to import a module dynamically at runtime. This can be useful if you are not sure which module needs to be imported until runtime. For example:

import importlib

def foo():
    module = importlib.import_module("module1")
    module.bar()
A tutorial on how to fix Python circular import. | Video: InfoWorld

More on PythonGarbage Collection and Memory Management in Python

 

5. Create a Common File to Import From

Another option is to create a common file that both modules can import from. This can help to break the cycle of imports and avoid circular imports. For example:

# common.py
def foo():
    pass

def bar():
    pass

# module1.py
from common import foo
def baz():
    foo()

# module2.py
from common import bar
def qux():
    bar()

Now instead of wasting hours trying to figure out what circular imports mean, you can try using any of these methods and you’ll be done in a jiffy. I recommend the fifth method. It works like a charm. 

SHARE