当许多.py文件导入重复的软件包时,如何保持标准的软件工程?

2024-05-01 21:42:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一些实用程序/工具.py文件,但它导入了许多重复的包。我将把这些.py文件导入其余的.py文件中

例如:

# tool_a.py
import a
from b import b_1
import c
from e import e_1
# some implementations in a.py.
... ...
... ...

# tool_b.py
import b
import c
from d import d_1
# some implementations in b.py
... ...
... ...

# tool_c.py
import c
import d
import e
# some implementations c.py
... ...
... ...

# And there are so many tools files like this
... ...
... ...
# Class A
import tool_a
import tool_b
# some implementations in Class A
... ...
... ...


# Class B
import tool_a
import tool_c
# some implementations in Class B
... ...
... ...

# And there are so many Class implementation files will include these tool files just like here
... ...
... ...

因此,如果我只是在每个类文件中逐个导入这些工具文件,它将导入大量重复的包,并导致依赖性问题

当然这是一个糟糕的方法,但我不知道如何解决可怕的包依赖性问题

有人能帮我吗


1条回答
网友
1楼 · 发布于 2024-05-01 21:42:24

我认为你不必担心多重进口。如果您告诉python导入一个已经导入的模块,它将只从最初导入时访问缓存的模块。最佳实践是只在需要的地方导入模块,而不必担心同一模块是否导入到多个文件中

相关问题 更多 >