Sick Gaming
[Tut] Python Import Error (ModuleNotFoundError) - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Programming (https://www.sickgaming.net/forum-76.html)
+--- Forum: Python (https://www.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)

<div><p class="has-pale-cyan-blue-background-color has-background">Python’s ImportError (<code>ModuleNotFoundError</code>) 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</code> to the directory and then adding this directory to <code>$PYTHONPATH</code>. If this file (<code>__init__.py</code>) is in the folder, change the position of the import in the file that is being imported from top to bottom.</p>
<h2>Why does Python ImportError Occur?</h2>
<p>An <code>ImportError</code> is detected when Python has problems with a successful module import. Usually this problem is caused by the incorrect <a href="https://blog.finxter.com/how-to-import-a-module-given-the-full-path/" target="_blank" rel="noreferrer noopener" title="How To Import A Module Given The Full Path">path </a>and is usually displayed with the message that there is <em>“No module named (…)”</em> or <em>“cannot import name (…)”</em>.</p>
<p>You can see an interactive example in our online browser project:</p>
<p> <iframe height="600px" width="100%" src="https://repl.it/@finxter/SpringgreenUsedLink?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe> </p>
<p><em><strong>Exercise</strong>: Try to fix the error message by following the hint in the comment!</em></p>
<p>So what can we do with this error?</p>
<h2>Creating Local Package</h2>
<p>If the ImportError is raised (<em>ImportError: No module named (…)</em>), when you import your own script, you need to check if the script you want to import has a file named <code>__init__.py</code> in its directory, if it does not, then you need to create it, because files named <code>__init__.py</code> are used to mark directories on the disk as directories of Python packages, and directories without such file are ignored.</p>
<p>To add this file simply create a text document named <code>__init__</code> in your folder and change its extension to <code>.py</code> => <code>__init__.py</code>.</p>
<p><strong>Note</strong>: Remember that the <code>__init__.py</code> file cannot have any other characters in its name!!!</p>
<h2>Adding Your Package To Path</h2>
<p>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).&nbsp;</p>
<p><strong>The question is: How can the Path be found?</strong></p>
<p>The easiest way to find the path is to write the following script:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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']</pre>
<p>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.</p>
<h2>When you have several files that import each other</h2>
<p>Sometimes in Python, even if you have a <code>__init__.py</code> 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:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""> #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()</pre>
<p>Output will be the following – <code>ImportError: cannot import name 'a1'</code>.<br />But if we change the position of <code>from test.b import b2</code> in A like below:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def a1(): print('a1') b2()
from test.b import b2
</pre>
<p>Then we can get what we want:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">b1
a1
b2</pre>
<h2>Summary</h2>
<p>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</code>), 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 (...)</code>).</p>
<p>I hope this article helped you understand why this error occurred in your file and gave you a clue to remove it.</p>
<p>The post <a href="https://blog.finxter.com/python-import-error-modulenotfounderror/" target="_blank" rel="noopener noreferrer">Python Import Error (ModuleNotFoundError)</a> first appeared on <a href="https://blog.finxter.com/" target="_blank" rel="noopener noreferrer">Finxter</a>.</p>
</div>


https://www.sickgaming.net/blog/2020/09/20/python-import-error-modulenotfounderror/