Circular import dependency in Python

后端 未结 7 2182
小蘑菇
小蘑菇 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:10

    You may defer the import, for example in a/__init__.py:

    def my_function():
        from a.b.c import Blah
        return Blah()
    

    that is, defer the import until it is really needed. However, I would also have a close look at my package definitions/uses, as a cyclic dependency like the one pointed out might indicate a design problem.

提交回复
热议问题