Circular dependency on Python OOP

Here is my import structure:

inputs.py (It includes user inputs and it calls the simulator where simulator runs)

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

simEngine.py

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

Building.py

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

ResidentArrivalSetup.py

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)

Resident.py

import inputs
  
class Resident:
    def __init__(self, X, Y):
        self.X = X
        self.Y = Y
        self.buildingHeight = inputs.buildingHeight
  1. When I run the inputs.py, it gets the input from the user and it calls simulator function.
  2. Then simulator calls Building and ResidentArrivalSetup classes (Building.py is constructed from the inputs.py).
  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 inputs.py, 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: https://stackoverflow.com/questions/66341377/circular-dependency-on-python-oop

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: