How to Avoid Circular Imports in Python?

Imagine you and your friend are trying to decide who goes through the door first. You say, “After you,” and your friend says, “No, no, after you.” And there you both stand… forever.

In Python, this happens when Module A needs something from Module B, but Module B also needs something from Module A. It’s a standoff, and nobody wins.

Example of Circular Import

πŸ’‘ Example: Imagine you have two Python files: vehicles.py and garage.py. In vehicles.py, you define a class Car that needs to check if a given car is currently stored in a garage from garage.py.

Conversely, garage.py defines a class Garage that stores cars, and for some functionality, it needs to create car instances using the Car class defined in vehicles.py. If both files attempt to import each other at the top, Python will stumble into a circular import.

When you try to run one of the modules, Python will try to load vehicles.py, which in turn tries to load garage.py before it has finished loading vehicles.py, leading to errors or unexpected behavior because one module is trying to use parts of the other before they are fully available.

To illustrate the circular import issue with a technical example, let’s create the code for the vehicles.py and garage.py scenario mentioned earlier.

vehicles.py

from garage import Garage  # This causes a circular import

class Car:
    def __init__(self, model):
        self.model = model

    def is_in_garage(self):
        # Checks if the car is in the garage
        return Garage().contains(self.model)

garage.py

from vehicles import Car  # This causes a circular import

class Garage:
    def __init__(self):
        self.cars = []

    def add_car(self, model):
        car = Car(model)  # Creating a Car instance
        self.cars.append(car)

    def contains(self, model):
        return any(car.model == model for car in self.cars)

In this scenario, vehicles.py imports Garage from garage.py at the top level, and garage.py does the same with Car from vehicles.py, creating a circular import problem. When either vehicles.py or garage.py is run, Python encounters an import statement that leads it into a loop, causing errors because it tries to access functionality from a module that has not been fully loaded yet.

General Solution Method

To resolve the circular import issue in our vehicles.py and garage.py example, one effective solution is to move the import statements inside the functions or methods where they are actually needed. This way, the import happens at runtime, when the function is called, rather than at the module’s load time, thereby avoiding the circular dependency problem. This method is particularly useful when the imported functionality is not required immediately when the module is loaded but rather at a specific point during execution, such as within a method’s body.

Fixed vehicles.py

# Removed the import from the top to inside the method
class Car:
    def __init__(self, model):
        self.model = model

    def is_in_garage(self):
        from garage import Garage  # Import moved inside the method
        return Garage().contains(self.model)

Fixed garage.py

# Kept the import of Car inside the method that needs it
class Garage:
    def __init__(self):
        self.cars = []

    def add_car(self, model):
        from vehicles import Car  # Import moved inside the method
        car = Car(model)  # Creating a Car instance
        self.cars.append(car)

    def contains(self, model):
        return any(car.model == model for car in self.cars)

With this approach, the circular import is resolved because Garage is only imported within Car.is_in_garage() when it’s needed, and similarly, Car is imported within Garage.add_car() only at the point of use. This ensures that both classes can be defined without requiring each other at the top level, thus avoiding the loop of imports.

Let’s have a look at multiple (alternative) solution methods next! πŸ‘‡πŸ‘‡πŸ‘‡

Method 1: Merge Together

If Module A and Module B are inseparable, why not combine them into one big Module C? It simplifies things!

Example:

# Before: A.py and B.py are two separate files causing issues.
# After: Combine into C.py where everything lives happily ever after.

Method 2: Take Turns

What if you took turns? Move the import inside a function. This way, the code only tries to import when the function is called, potentially avoiding the standoff.

Example:

# In A.py
def dance_with_b():
    from B import b_dance
    b_dance()

Method 3: Change the Routine

Sometimes, the dance steps are too complicated. Maybe Module A and Module B don’t need to be so dependent on each other. Split them up or reorganize the steps so that they flow in one direction.

Example:

# Instead of A importing B and B importing A,
# Split B into B1 and B2 where B1 can safely import A and B2 does not.

Method 4: Use a Choreographer πŸ“

Packages in Python can use an __init__.py file to manage imports like a choreographer managing dancers. It can help organize and control how modules interact with each other.

Example:

# Inside your package's __init__.py
from .A import A
from .B import B

Method 5: Dance by Proxy 🎭

Inversion of Control (IoC) is like hiring a dance proxy. Instead of Module A and B directly interacting, they pass messages or objects through an intermediary. It’s a bit like sending love letters instead of talking directly.

Example:

# A and B communicate through an intermediary, avoiding direct imports.

Method 6: Sign Language 🀟

Abstract Base Classes (ABCs) work like sign language for modules. They define a common language (interface) that both modules can understand without directly communicating.

Example:

# ABCs.py defines a common interface.
# A.py and B.py both import ABCs.py but not each other.