Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python Import Error (ModuleNotFoundError)
1
Python Import Error (ModuleNotFoundError)

Python’s ImportError (
Code:
ModuleNotFoundError
) indicates that you tried to import a module that Python doesn’t find. It can usually be eliminated by adding a file named
Code:
__init__.py
to the directory and then adding this directory to
Code:
$PYTHONPATH
. If this file (
Code:
__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
Code:
ImportError
is detected when Python has problems with a successful module import. Usually this problem is caused by the incorrect pathand 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:

https://repl.it/@finxter/SpringgreenUsedLink?lite=true

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
Code:
__init__.py
in its directory, if it does not, then you need to create it, because files named
Code:
__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
Code:
__init__
in your folder and change its extension to
Code:
.py
=>
Code:
__init__.py
.

Note: Remember that the
Code:
__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:

Code:
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
Code:
__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:

Code:
#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 –
Code:
ImportError: cannot import name 'a1'
.
But if we change the position of
Code:
from test.b import b2
in A like below:

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

Then we can get what we want:

Code:
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
Code:
__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 (
Code:
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.

The post Python Import Error (ModuleNotFoundError) first appeared on Finxter.
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: