Circular dependency on Python OOP

Here is my import structure: (It includes user inputs and it calls the simulator where simulator runs)

from simEngine import simulator
# user inputs
simulator(A, B)

import Building
import ResidentArrivalSetup
def simulator(A, B):
    building = Building(...)
    resident = ResidentArrivalSetup(...)

import inputs
class Building:
    def __init__(self, blaBla)
        self.blaBla = blaBla
        self.residentDic = {}

import Resident
import Building
class ResidentArrivalSetup:
    def __init___(self, C):
        self.C = C
        self.building = Building(residentDic)
    def someMethod(self):
         resident = Resident(K,L,M)

import inputs
class Resident:
    def __init__(self, X, Y):
        self.X = X
        self.Y = Y
        self.buildingHeight = inputs.buildingHeight
  1. When I run the, it gets the input from the user and it calls simulator function.
  2. Then simulator calls Building and ResidentArrivalSetup classes ( is constructed from the
  3. ResidentArrivalSetup creates an instance of Resident. Building has a dictionary where I store the Resident instances when I create a Resident from ResidentArrivalSetup . So, I import that dictionary by importing Building into ResidentArrivalSetup.

When I run the, I get

ImportError: cannot import name 'simulator' from 'simEngine'

I added the current path to the sys.path already. I think my problem is about circular dependency. How can I solve this?

Read more here:

Content Attribution

This content was originally published by kerim at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: