这可能在其他地方得到回答,但是我找不到解决方案。
我有以下两个文件夹结构:
app/
service/
__init__.py
caller.py
...
utils/
__init_.py
config.py
...
__init__.py
...
在caller.py
我有:
from app.utils import config
当我运行python caller.py
(从cd
到app/service
)时,我得到:
from app.utils import config
ModuleNotFoundError: No module named 'app'
但是,如果我移至caller.py
该app
文件夹并使用以下导入行:
from utils import config
有用。为什么?我也曾尝试使用.app.utils
,..utils
等,但无济于事。
如果将父目录添加到该路径,则在切换到目录“ / app / service”时,Python将找到相邻的“ utils”目录。
import sys
sys.path.insert(1, '../')
from utils import config
print(f"Caller imported config value SETTING_X: {config.SETTING_X}")
SETTING_X = 42
Caller imported config value SETTING_X: 42
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句