Create an account


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 (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.

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



https://www.sickgaming.net/blog/2020/09/...ounderror/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How To Kill A Thread In Python? xSicKxBot 0 6 10-24-2020, 05:40 AM
Last Post: xSicKxBot
  [Tut] Python’s NameError: name ‘xxx’ is not defined — How to Fix This Stupid Bug? xSicKxBot 0 12 10-16-2020, 10:30 PM
Last Post: xSicKxBot
  [Tut] Python IndentationError: unexpected indent (How to Fix This Stupid Bug) xSicKxBot 0 18 10-10-2020, 08:24 PM
Last Post: xSicKxBot
  [Tut] Yield Keyword in Python – A Simple Illustrated Guide xSicKxBot 0 19 10-09-2020, 06:13 PM
Last Post: xSicKxBot
  [Tut] Python Reverse List with Slicing — An Illustrated Guide xSicKxBot 0 36 10-02-2020, 04:24 AM
Last Post: xSicKxBot
  [Tut] How to Remove Duplicates From a Python List While Preserving Order? xSicKxBot 0 35 10-01-2020, 02:21 AM
Last Post: xSicKxBot
  [Tut] How To Update A Key In A Dictionary In Python If The Key Doesn’t Exist? xSicKxBot 0 40 09-29-2020, 09:02 PM
Last Post: xSicKxBot
  [Tut] How to Get the Last Element of a Python List? xSicKxBot 0 42 09-28-2020, 02:36 AM
Last Post: xSicKxBot
  [Tut] How To Format A String That Contains Curly Braces In Python? xSicKxBot 0 34 09-25-2020, 07:51 PM
Last Post: xSicKxBot
  [Tut] Python One Line Dictionary xSicKxBot 0 41 09-21-2020, 05:58 PM
Last Post: xSicKxBot

Forum Jump:

Become a Patron!
[-]
Upcoming Events

[-]
Latest Threads
News - Review: Tennis World Tour 2 – A P...
Last Post: xSicKxBot
Today 03:06 AM
» Replies: 0
» Views: 1
News - Disneyland Is Sort Of Opening, Bu...
Last Post: xSicKxBot
Today 03:06 AM
» Replies: 0
» Views: 2
[1.06] BO4 Zombie Trainer (Call of duty ...
Last Post: poochygoo
Today 01:25 AM
» Replies: 13
» Views: 444
[Tut] How to Build Your Brand as a Freel...
Last Post: xSicKxBot
Yesterday 08:58 PM
» Replies: 0
» Views: 2
(Indie Deal) FREE Ghost Sweeper, ? NBA 2...
Last Post: xSicKxBot
Yesterday 08:58 PM
» Replies: 0
» Views: 5
Drag[en]gine Hands-On
Last Post: xSicKxBot
Yesterday 08:58 PM
» Replies: 0
» Views: 3
AppleInsider - Apple TV Remote app influ...
Last Post: xSicKxBot
Yesterday 08:58 PM
» Replies: 0
» Views: 3
News - Nightdive Studios Delays Its Enha...
Last Post: xSicKxBot
Yesterday 08:58 PM
» Replies: 0
» Views: 2
News - PS5 Third-Party Face Plates Are A...
Last Post: xSicKxBot
Yesterday 08:58 PM
» Replies: 0
» Views: 3
Xbox Wire - Xbox Insider Release Notes –...
Last Post: xSicKxBot
Yesterday 02:31 PM
» Replies: 0
» Views: 6

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020