Python Import Error (ModuleNotFoundError)

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. If this file (__init__.py) is in the folder, change the position of the import in the file that is being imported from top to bottom.

Why does Python ImportError Occur?

An ImportError is detected when Python has problems with a successful module import. Usually this problem is caused by the incorrect path and is usually displayed with the message that there is “No module named (…)” or “cannot import name (…)”.

You can see an interactive example in our online browser project:

Exercise: Try to fix the error message by following the hint in the comment!

So what can we do with this error?

Creating Local Package

If the ImportError is raised (ImportError: No module named (…)), when you import your own script, you need to check if the script you want to import has a file named __init__.py in its directory, if it does not, then you need to create it, because files named __init__.py are used to mark directories on the disk as directories of Python packages, and directories without such file are ignored.

To add this file simply create a text document named __init__ in your folder and change its extension to .py => __init__.py.

Note: Remember that the __init__.py file cannot have any other characters in its name!!!

Adding Your Package To Path

When you want to add your module to the path permanently, you need to find the path to the site-packages folder and add the folder containing your module to this or another folder (where of course Python looks for modules). 

The question is: How can the Path be found?

The easiest way to find the path is to write the following script:

import sys
print(sys.path)

# Output:
[‘PathToYourFolders’, 'C:\\Users\\YourUsername\\AppData\\Local\\Programs\\Python\\Python38\\python38.zip', 'C:\\Users\\YourUsername\\AppData\\Local\\Programs\\Python\\Python38\\DLLs', 'C:\\Users\\YourUsername\\AppData\\Local\\Programs\\Python\\Python38\\lib', 'C:\\Users\\YourUsername\\AppData\\Local\\Programs\\Python\\Python38', 'C:\\Users\\YourUsername\\AppData\\Roaming\\Python\\Python38\\site-packages', 'C:\\Users\\YourUsername\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages']

Then we see all the paths in which Python looks for modules, just add your module to one of them (the best …\lib\site-packages). Once we do this, we will be able to call the module from any Python script.

When you have several files that import each other

Sometimes in Python, even if you have a __init__.py file in your folder, the ImportError occurs, it says that name cannot be imported. To eliminate this problem, the order of imports must be changed. The code causing the error:

       #a2.py file
from test.b import b2
def a1():
    print('a1')
    b2()
from test.a import a1
       #b2.py file
def b1():
    print('b1')
    a1()
def b2():
    print('b2')
if __name__ == '__main__':
    b1()

Output will be the following – ImportError: cannot import name 'a1'.
But if we change the position of from test.b import b2 in A like below:

def a1():
    print('a1')
    b2()
from test.b import b2

Then we can get what we want:

b1
a1
b2

Summary

At the beginning we explained how to solve the problem from the title, and then we explained why the import error occurs. Then three ways of action were presented. First described how to make a local package (adding __init__.py), second how to make a package that can be called from any Python script (add the module to the site-packages folder) and third what to do when you have several files that import each other (ImportError: cannot import name (...)).

I hope this article helped you understand why this error occurred in your file and gave you a clue to remove it.