我有以下文件夹结构:
top folder
- final_program.py
- scripts
- __init__.py
- extract.py
- estimations.py
- calculate_all.py
final_program.py在calculate_all.py中运行一个函数。所以,我有这个:
from scripts.calculate_all import get_info
但是,calculate_all.py脚本使用extract.py和estimatetions.py脚本。所以,在calculate_all.py中,我有这个:
import scripts.estimations
import scripts.extract
直接运行final_program.py时,我遇到了没有错误。大。但是,我还希望能够直接运行calculate_all.py。当我尝试时,我收到此错误:
ModuleNotFoundError:没有名为'scripts.estimations'的模块
我该如何解决这个问题?
这对我有用:
__init__.py
from . import estimations
from . import extract
import scripts.calculate_all
import scripts.extract
使用-m
标志运行python文件(将模块作为脚本运行)。
对于final_program:python -m final_program
对于calculate_all:python -m scripts.calculate_all
在calculate_all.py
中使用
import .estimations
import .extract