Python 'No module named' error; 'package' is not a package

I ran into something similar and the answer from OP about namespace collision is what finally clued me in.

I was using the same name for both a sub-package (directory) and a module (file) within it.

For example I had this:

/opt/mylib/myapi
/opt/mylib/myapi/__init__.py
/opt/mylib/myapi/myapi_creds.py        # gitignored file for user/pass
/opt/mylib/myapi/myapi.py              # base module, load creds and connect
/opt/mylib/myapi/myapi_dostuff.py      # call myapi.py and do work

The script 'myapi.py' imports credentials from myapi_creds.py via this statement:

from myapi.myapi_creds import my_user, my_pass

Testing the module 'myapi.py' resulted in this error:

$ ./myapi.py
Traceback (most recent call last):
  File "./myapi.py", line 12, in <module>
    from myapi.myapi_creds import my_user, my_pass
  File "/opt/mylib/myapi/myapi.py", line 12, in <module>
    from myapi.myapi_creds import my_user, my_pass
ModuleNotFoundError: No module named 'myapi.myapi_creds'; 'myapi' is not a package

The solution was to rename myapi.py to myapi_base.py so it's name does not collide with the sub-package name.


The issue was in the naming of my file.

I hastily named my file emailage.py and then tried to import from emailage.client.

I'm assuming that Python looked in my current directory and matched the names of the file I was working on before checking the installed third party libraries.

After renaming my file everything seems ok.

For others who run into similar problems -- beware of conflicting naming. Sometimes the simplest things trip you up the longest.