最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I handle circular dependencies in Python when using import in a large project? - Stack Overflow

programmeradmin1浏览0评论

I’m working on a large Python project, and I’ve come across a situation where two or more modules have circular dependencies. For example, module A imports module B, and module B imports module A (directly or indirectly). This is causing an ImportError because the interpreter can’t resolve the dependencies. I have the following structure: Module A

from module_b import func_b
def func_a():
    print("This is function A")
    func_b() 

Module B

from module_a import func_a

def func_b():
    print("This is function B")
    func_a()

When I try to run the code, I get the error:

ImportError: cannot import name 'func_a' from partially initialized module 'module_a'

My Questions related to this?

  1. What is the best way to handle circular imports in Python, especially when modules need to reference each other?
  2. Are there any design patterns I should follow to avoid circular dependencies in the first place?
  3. Would breaking the modules into smaller, more focused submodules help prevent this issue, or is there a more Pythonic way to deal with circular imports in large projects?

I’m working on a large Python project, and I’ve come across a situation where two or more modules have circular dependencies. For example, module A imports module B, and module B imports module A (directly or indirectly). This is causing an ImportError because the interpreter can’t resolve the dependencies. I have the following structure: Module A

from module_b import func_b
def func_a():
    print("This is function A")
    func_b() 

Module B

from module_a import func_a

def func_b():
    print("This is function B")
    func_a()

When I try to run the code, I get the error:

ImportError: cannot import name 'func_a' from partially initialized module 'module_a'

My Questions related to this?

  1. What is the best way to handle circular imports in Python, especially when modules need to reference each other?
  2. Are there any design patterns I should follow to avoid circular dependencies in the first place?
  3. Would breaking the modules into smaller, more focused submodules help prevent this issue, or is there a more Pythonic way to deal with circular imports in large projects?
Share Improve this question edited Jan 21 at 2:25 couteau 3192 silver badges7 bronze badges asked Jan 20 at 11:58 Eric WalterEric Walter 12 bronze badges 1
  • This question is similar to: What happens when using mutual or circular (cyclic) imports?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – dimakin Commented Jan 20 at 15:45
Add a comment  | 

1 Answer 1

Reset to default 0
  1. Use Import Statements Inside Functions
# module_a.py
def func_a():
    from module_b import func_b  # Import moved inside the function
    print("This is function A")
    func_b()

# module_b.py
def func_b():
    from module_a import func_a  # Import moved inside the function
    print("This is function B")
    func_a()
  1. Conditional Imports with if TYPE_CHECKING (Only if you're using a function or an import for type checking related tasks), this make's it so that the specific import is imported in type checking and not the runtime it self
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from module_b import func_b
发布评论

评论列表(0)

  1. 暂无评论