我有以下文档结构:
root
|src
|__init__.py
|util.py
|mymodule.py
|__init__.py
|test.py
两个init.py都是空的
在 util.py 中,我有一系列实用函数,例如 a()、b()、c()。
在 mymodule.py 中我有:
from util import a, b, c
def somefunc():
# some stuff
a()
# more stuff
在 test.py 中我有:
from src.mymodule import somefunc
我希望能够在 src 之外引用这些模块,但收到错误“error in mymodule.py from util import a, b, c, ModuleNotFoundError: No module named 'util'”
最佳答案
您需要使用模块路径导入 utils,例如:from src.util import a, b, c
或 from root.src.util import a, b, c
> 取决于您计划从何处运行/导入代码,后者适用于您从包含 root 的文件夹运行/导入代码
关于Python导入模块导入另一个模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55977216/