Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 362
Aaron Taveras Avatar asked Jan 23 '19 18:01

Aaron Taveras


People also ask

How do I resolve ImportError?

If the error occurs due to a circular dependency, it can be resolved by moving the imported classes to a third file and importing them from this file. If the error occurs due to a misspelled name, the name of the class in the Python file should be verified and corrected.

How do you fix ModuleNotFoundError and ImportError?

Python's ImportError ( ModuleNotFoundError ) indicates that you tried to import a module that Python doesn't find. It can usually be eliminated by adding a file named __init__.py to the directory and then adding this directory to $PYTHONPATH .

What does __ init __ py do in Python?

The __init__.py files are required to make Python treat directories containing the file as packages. This prevents directories with a common name, such as string , unintentionally hiding valid modules that occur later on the module search path.


3 Answers

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.

like image 95
Aaron Taveras Avatar answered Oct 18 '22 00:10

Aaron Taveras


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.

like image 23
refriedjello Avatar answered Oct 18 '22 00:10

refriedjello


I took a look at this problem, and even though it is not exactly the same error that I encountered, it helped me solve it. I'll explain the situation I had, since I think some users might find this handy. So, I was getting the following error log:

    Traceback (most recent call last):
  File "/home/kemal/Programming/Python/Preference_Articulation/LocalSearch/LS_apriori.py", line 1, in <module>
    from LocalSearch.LocalSearch import LocalSearch
ModuleNotFoundError: No module named 'LocalSearch.LocalSearch'; 'LocalSearch' is not a package

The structure of my project is the following (using PyCharm): View of project structure

The important thing to notice is that I separated my code into several folders, since it makes it more readable. Now, in the folder named LocalSearch I have 4 files, LocalSearch, LS_apriori and some 2 tests files (not relevant). When trying to run the file LS_apriori (which uses methods and classes from file LocalSearch) I was getting the error provided above. The code specifically is not important, and the way I handled the imports was the following:

from LocalSearch.LocalSearch import LocalSearch

The fix was simple. I renamed the py-file LocalSearch to Local_Search (just added an underscore). Afterwards, the error was gone.

So my problem was possessing a folder(package) with the same name as a file(module) inside it, which has a class inside it with the same name. Python didn't like that. Having modules with the same name as packages inside them is fine however, I guess the class just added extra confusion.

like image 2
waykiki Avatar answered Oct 18 '22 02:10

waykiki