导入错误,没有名为xxxx的模块

时间:2022-06-05 18:10:04

I have a project having the structure

我有一个具有该结构的项目

/example
../prediction
....__init__.py
....a.py

PYTHONPATH is pointed to /example

PYTHONPATH指向/示例

now I open the python in terminal and type

现在我在终端打开python并输入

import prediction

it succeeded, but if I type

它成功了,但如果我输入

import prediction.a

it returns error

它返回错误

ImportError: No module named 'prediction.a'; 'prediction' is not a package

why is that? isn't that already imported as a package

这是为什么?不是已经作为包导入

1 个解决方案

#1


33  

The behavior you are seeing can be caused if there is a module (foo.py) or package (foo/__init__.py) in your current directory that has a conflicting name.

如果当前目录中存在具有冲突名称的模块(foo.py)或包(foo / __ init__.py),则可能会导致您看到的行为。

In your case, I suspect there is a file named prediction.py, and you're getting that instead of the prediction package in your examples directory.

在你的情况下,我怀疑有一个名为prediction.py的文件,你得到的不是你的examples目录中的预测包。

#1


33  

The behavior you are seeing can be caused if there is a module (foo.py) or package (foo/__init__.py) in your current directory that has a conflicting name.

如果当前目录中存在具有冲突名称的模块(foo.py)或包(foo / __ init__.py),则可能会导致您看到的行为。

In your case, I suspect there is a file named prediction.py, and you're getting that instead of the prediction package in your examples directory.

在你的情况下,我怀疑有一个名为prediction.py的文件,你得到的不是你的examples目录中的预测包。