将模块动态导入python函数

将模块动态导入python函数,python,import,parameters,module,Python,Import,Parameters,Module,我有这个项目结构 utilities.py /references module1.py module2.py ... 在utilities.py文件中,我希望有一个带有如下参数的动态导入函数: def import(self, file): from references.file import file ... 进入我使用的utilities.py文件 import("module1") 但它不起作用,我有以下错误 from ref

我有这个项目结构

utilities.py 
/references
    module1.py
    module2.py
    ...
在utilities.py文件中,我希望有一个带有如下参数的动态导入函数:

def import(self, file):    
    from references.file import file  
    ...
进入我使用的utilities.py文件

import("module1")
但它不起作用,我有以下错误

from references.file import steps ModuleNotFoundError: No module named 'references.file'

我需要一些帮助,谢谢

如果要使用字符串名称导入,请尝试该函数

您可以使用一个类似以下功能:

def dynamic_import(file):
    importlib.import_module(f'references.{file}')
对于更多的控制,你可以使用内置的,但这是更多的参与,并不是真正的建议