Circular import dependency in Python

后端 未结 7 2125
小蘑菇
小蘑菇 2020-11-22 16:08

Let\'s say I have the following directory structure:

a\\
    __init__.py
    b\\
        __init__.py
        c\\
            __init__.py
            c_file.p         


        
7条回答
  •  自闭症患者
    2020-11-22 17:08

    I've wondered this a couple times (usually while dealing with models that need to know about each other). The simple solution is just to import the whole module, then reference the thing that you need.

    So instead of doing

    from models import Student
    

    in one, and

    from models import Classroom
    

    in the other, just do

    import models
    

    in one of them, then call models.Classroom when you need it.

提交回复
热议问题