Import error, No module named xxxx

PythonPython 3.x

Python Problem Overview


I have a project having the structure

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

PYTHONPATH is pointed to /example

now I open the python in terminal and type

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

Python Solutions


Solution 1 - Python

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.

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHello ladView Question on Stackoverflow
Solution 1 - PythonlarsksView Answer on Stackoverflow