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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31279446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:45:08  来源:igfitidea点击:

Import error, No module named xxxx

pythonpython-3.x

提问by Hello lad

I have a project having the structure

我有一个具有结构的项目

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

PYTHONPATH is pointed to /example

PYTHONPATH 指向 /example

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

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

采纳答案by larsks

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 predictionpackage in your examplesdirectory.

在您的情况下,我怀疑有一个名为 的文件prediction.py,并且您正在获取该文件而不是目录中的predictionexamples