別の解決策は、d_fileにプロキシを使用することです。
たとえば、blahクラスをc_fileと共有したいとします。したがって、d_fileには次のものが含まれます。
class blah:
def __init__(self):
print("blah")
c_file.pyに入力する内容は次のとおりです。
# do not import the d_file !
# instead, use a place holder for the proxy of d_file
# it will be set by a's __init__.py after imports are done
d_file = None
def c_blah(): # a function that calls d_file's blah
d_file.blah()
そして、のinit .py:
from b.c import c_file
from b.d import d_file
class Proxy(object): # module proxy
pass
d_file_proxy = Proxy()
# now you need to explicitly list the class(es) exposed by d_file
d_file_proxy.blah = d_file.blah
# finally, share the proxy with c_file
c_file.d_file = d_file_proxy
# c_file is now able to call d_file.blah
c_file.c_blah()