Question

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

I'm trying to make a simple import and use the emailage third party library.

As per their documentation, the way to use their library is as follows:

pip install emailage-official

Then, simply import with:

from emailage.client import EmailageClient

The install works fine with pip - no errors. I double checked to see that the emailage package exists within the proper directory, and it does.

Package exists at:

C:\Users\aaron\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\emailage

This folder has (seemingly) the correct files with an __init__.py and everything. However, both pylint and command line interpreter throw me a 'No module named 'emailage.client'; 'emailage' is not a package' error.

The output of my sys.path is:

[... 
'C:\\Users\\aaron\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages'
...
]

So the directory where emailage is installed is a part of the path... and lastly I pip-installed numpy just to test if it worked properly. Numpy installed to the same site-packages folder as emailage, and it works fine when it is imported, so I'm stuck.

I don't typically use Python much, so any and all help would be appreciated.

 46  90517  46
1 Jan 1970

Solution

 88

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.

2019-01-23

Solution

 38

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.

2019-12-16