我的程序使用importlib.
这个导入的模块(我们称之为A)导入位于它旁边的一些其他自定义模块(我们称之为B)。
My_Project\
│
└─── my_program.py
Some_Other_Location\
│
├─── A_module_my_program_wants_to_import.py
└─── B_module_A_imports.py
当我导入A 而不导入B时,它工作得很好:
# Sample from my_program.py
path = Some_Other_Location\A_module_my_program_wants_to_import.py
spec = importlib.util.spec_from_file_location("A", path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
但是,当在A我导入B:
# Sample from A_module_my_program_wants_to_import.py
import B_module_A_imports
或者
from B_module_A_imports import foo
我运行我的程序,我得到:
Build error: No module named 'B_module_A_imports'
回溯到我在程序中导入的位置,以及
我试过指定submodule_search_locations=Some_Other_Location,spec_from_file_location但没有帮助。
所以问题是如何导入远程模块,即导入本地模块?
绝地无双
相关分类