我试图弄清楚如何以编程方式执行包含相对导入的模块。
伪代码
spec = importlib.util.spec_from_file_location(name, path)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
Run Code Online (Sandbox Code Playgroud)
其中 name 是类,path 是 .py 的绝对路径
当正在加载的模块包含相对导入时,对 exec_module 的调用将引发以下异常:
尝试在没有已知父包的情况下进行相对导入
有没有办法以编程方式执行本身包含相对导入的 python 模块?如果是这样,怎么办?