我有这样的文件:
dir
+ main.py
+--a
| +a.py
|
+--b
| +b.py
|
+--c
+c.py
在{
from b import b
funcs = b.loadAllFromC()
# some code which uses funcs
在b.py
中,我试图加载文件夹c
中的所有模块,并在每个模块中获取函数,以便在main.py
中使用:
import os
import importlib
def loadAllFromC():
modules = []
importlib.invalidate_caches()
for file in os.listdir("./c/"):
if file.endswith(".py"):
module = importlib.import_module("c." + file[:-3])
modules.append(module.getFunc())
return modules
在{
from a import a
def func():
# code which uses something from a
def getFunc():
return func
但是b.py
中的module.getFunc()
失败:
...
File "... /dir/c/c.py", line 1, in <module>
ModuleNotFoundError: No module named 'a'
当我尝试from ..a import a
时,我得到了ValueError: attempted relative import beyond top-level package
。
如何使用来自c.py
的a.py
目前没有回答
相关问题 更多 >
编程相关推荐