![]() |
|
[Tut] Python Import Error (ModuleNotFoundError) - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python Import Error (ModuleNotFoundError) (/thread-97348.html) |
[Tut] Python Import Error (ModuleNotFoundError) - xSicKxBot - 09-20-2020 Python Import Error (ModuleNotFoundError) 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. |