How to structure python modules/packages according to dependecy inversion

If I am working on a project, say it has this file structure: car/ body/ __init__.py doors.py bonnet.py engine/ cyclinderhead/ __init__.py pistons.py __init__.py crankshaft.py engconstants.py utilities.py Now invariabley the pistons.py file will need some sort of constant (engconstants.py) or utility function (utilities.py). The pistons.py is a very nested deep file. Is it okay that it depends on files higher up in the file structure? I got the impression that the less nested files should only depend on the more nested files, and depencies going the other way are a code smell? I often get myself into circular import predicaments and am trying to implement a set of rules/guidelines to prevent that.

Apr 21, 2025 - 01:36
 0
How to structure python modules/packages according to dependecy inversion

If I am working on a project, say it has this file structure:

car/
   body/
      __init__.py
      doors.py
      bonnet.py
   engine/
      cyclinderhead/
          __init__.py
          pistons.py
      __init__.py
      crankshaft.py
      engconstants.py
    utilities.py    

Now invariabley the pistons.py file will need some sort of constant (engconstants.py) or utility function (utilities.py). The pistons.py is a very nested deep file. Is it okay that it depends on files higher up in the file structure? I got the impression that the less nested files should only depend on the more nested files, and depencies going the other way are a code smell? I often get myself into circular import predicaments and am trying to implement a set of rules/guidelines to prevent that.