0

How to import both car and abstract_vehicle in main.py?

I keep getting circular dependencies error or ModuleNotFoundError.

Project structure

├── vehicles
│   ├── __init__.py
│   └── abstract_vehicle.py
│   └── car.py
│   └── motor.py
└── main.py

car.py

from abstract_vehicle import abstract_vehicle
class car(abstract_vehicle):
    #do sth

init.py

import abstract_vehicle
import car

main.py

from vehicles import car, abstract_vehicle #error

vehicle_a = car()
def do_sth(a:abstract_vehicle):
    pass

do_sth(vehicle_a)
Gameriker
  • 35
  • 7
  • Does this answer your question? [Circular dependency in Python](https://stackoverflow.com/questions/894864/circular-dependency-in-python) – Woodford Jul 18 '23 at 18:00
  • Notwithstanding the advice in the question linked to by @Woodford circular dependencies that cause import errors almost always indicate a design problem. In my three decades of writing Python I only worked on one project where this was a problem. We "solved" the immediate problem by using a hard to understand lazy import; i.e., a bandaid. We fixed it properly as soon as we could devote some resources to restructure the code. – Kurtis Rader Jul 18 '23 at 18:13

0 Answers0