【发布时间】:2022-08-08 18:08:13
【问题描述】:
我有一个具有以下结构的项目:
HorticulturalSalesPrediction/
Docker
HorticulturalSalesPrediction_API/
optimization/
__init__.py
optuna_optim.py
preprocess/
__init__.py
base_dataset.py
utils/
__init__.py
FeatureAdder.py
helper_functions.py
__init__.py
optim_pipeline.py
run.py
在脚本run.py 我导入这样的东西:
import optim_pipeline
from utils import helper_functions
在脚本optim_pipeline.py 中,我导入如下内容:
from utils import helper_functions
from preprocess import base_dataset
from optimization import optuna_optim
我使用 IDE PyCharm 开发了这个框架,当我使用 \'Run\'-Button 运行它时,框架运行良好。但是当我想在带有python3 run.py 或python3 -m run.py 的终端上运行它时,我收到以下错误:
Traceback (most recent call last):
File \"run.py\", line 3, in <module>
import optim_pipeline
File \"/home/josef/Schreibtisch/HorticulturalSalesPrediction/HorticulturalSalesPrediction/HorticulturalSalesPrediction_API/optim_pipeline.py\", line 4, in <module>
from preprocess import base_dataset
File \"/home/josef/Schreibtisch/HorticulturalSalesPrediction/HorticulturalSalesPrediction/HorticulturalSalesPrediction_API/preprocess/base_dataset.py\", line 8, in <module>
from HorticulturalSalesPrediction_API.utils import FeatureAdder
ModuleNotFoundError: No module named \'HorticulturalSalesPrediction_API\'
我知道对于整个 python 导入主题(Relative imports - ModuleNotFoundError: No module named x、Call a function from another file?、Relative imports for the billionth time、...)已经有很多问题和解决方案,但这些都不适合我。
当我打印sys.path 时,我会收到\'/home/josef/Schreibtisch/HorticulturalSalesPrediction/HorticulturalSalesPrediction/HorticulturalSalesPrediction_API\',所以所有这些东西都应该在系统路径中可用。
我还尝试进行相对和绝对导入。但是通过这些尝试,我收到ValueError: attempted relative import beyond top-level package 或ImportError: attempted relative import with no known parent package 错误(例如,当我尝试from . import optim_pipeline 时)。
-
您是否尝试将 script.py 作为
python -m script声明启动?你在shell中输入什么? -
是的,我也尝试过
python3 -m run.py,但遇到了与运行python3 run.py相同的错误ModuleNotFoundError
标签: python path python-import importerror modulenotfounderror