【发布时间】:2023-01-21 12:18:19
【问题描述】:
我有一个需要一段时间才能导入的模块,我们称它为 big_module。该模块创建了我在其他文件中需要的几个变量。我在我的许多帮助程序文件中使用这个模块,称为 helper1、helper2 等...
我有一个导入每个帮助文件的主文件,所以我的文件看起来像这样:
# helper1.py
import big_module
def do_stuff1(input):
# code that uses big_module
# helper2.py
import big_module
def do_stuff2(input):
# code that uses big_module
等等帮助文件。然后我有我的主文件:
# main.py
import helper1
import helper2
# and so on
data1 = [some data]
data2 = helper1.do_stuff1(data1)
data3 = helper1.do_stuff2(data2)
# and so on
当我导入每个助手,然后每个助手随后导入 big_module 时,big_module 是否每次都重新运行,导致我浪费时间,或者 python 是否缓存它或其他东西以便它只运行一次?如果在多个文件中导入它确实浪费时间,有没有只导入一次的好方法?
【问题讨论】:
标签: python performance import