73

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

Hello lad
  • 17,344
  • 46
  • 127
  • 200

1 Answers1

144

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.

Mr. Napik
  • 5,499
  • 3
  • 24
  • 18
larsks
  • 277,717
  • 41
  • 399
  • 399
  • 6
    I've been bagging my head around to workaround a similar issue... this solved my problem. Thank you. – Alexis.Rolland Nov 19 '17 at 07:00
  • 5
    As your answer does only explain the reason but does not present a solution to import a package unambiguously, does that mean that Python is unable to do so? – Robert Jan 03 '18 at 13:35
  • 2
    I guess it depends on what you mean. Python *does not* import files by an absolute pathname, but you are free to modify the module search path yourself to change the order (or locations) in which it searches for things. If you'd like to pursue that question in more detail, you're best is probably opening a new question here on stackoverflow. – larsks Jan 03 '18 at 16:36