09-20-2020, 01:15 PM
Python Import Error (ModuleNotFoundError)
Python’s ImportError () indicates that you tried to import a module that Python doesn’t find. It can usually be eliminated by adding a file named to the directory and then adding this directory to . If this file () 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 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 in its directory, if it does not, then you need to create it, because files named 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 in your folder and change its extension to => .
Note: Remember that the 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:
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 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:
Output will be the following –.
But if we change the position of in A like below:
Then we can get what we want:
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), 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 ().
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.
Python’s ImportError (
Code:
ModuleNotFoundErrorCode:
__init__.pyCode:
$PYTHONPATHCode:
__init__.pyWhy does Python ImportError Occur?
An
Code:
ImportErrorYou 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__.pyCode:
__init__.pyTo add this file simply create a text document named
Code:
__init__Code:
.pyCode:
__init__.pyNote: Remember that the
Code:
__init__.pyAdding 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__.pyCode:
#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 b2Code:
def a1(): print('a1') b2()
from test.b import b2Then we can get what we want:
Code:
b1
a1
b2At 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__.pyCode:
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.

