【问题标题】:ImportError and ModuleNotFoundError: how to get a script running from the command line?ImportError 和 ModuleNotFoundError:如何让脚本从命令行运行?
【发布时间】: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.pypython3 -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 xCall 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 packageImportError: 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


【解决方案1】:

尝试重新安装模块并更新 python。

更新园艺销售预测_API

【讨论】:

  • HorticulturalSalesPrediction_API 分别是我自己创建的文件夹或模块,所以它不是我安装和导入的外部包。
  • 尝试重新安装或重新制作它
【解决方案2】:

所以我发现了我的错误。 解决方案是在 HorticultureSalesPrediction 文件夹中运行 python3 -m HorticulturalSalesPrediction_API.run。 然后它按预期工作。 然后我不得不调整一些导入:

from HorticulturalSalesPrediction_API.utils import helper_functions
from . import optim_pipeline

from HorticulturalSalesPrediction_API.utils import helper_functions
from HorticulturalSalesPrediction_API.preprocess import base_dataset
from HorticulturalSalesPrediction_API.optimization import optuna_optim

【讨论】:

    猜你喜欢
    • 2013-06-09
    • 2014-03-29
    • 1970-01-01
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多