Sick Gaming
[Tut] TensorFlow ModuleNotFoundError: No Module Named ‘utils’ - 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] TensorFlow ModuleNotFoundError: No Module Named ‘utils’ (/thread-100298.html)



[Tut] TensorFlow ModuleNotFoundError: No Module Named ‘utils’ - xSicKxBot - 11-27-2022

TensorFlow ModuleNotFoundError: No Module Named ‘utils’

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;924941&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</div>
<h2>Problem Formulation</h2>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="570" height="102" src="https://blog.finxter.com/wp-content/uploads/2022/11/image-273.png" alt="" class="wp-image-924946" srcset="https://blog.finxter.com/wp-content/uploads/2022/11/image-273.png 570w, https://blog.finxter.com/wp-content/uploads/2022/11/image-273-300x54.png 300w" sizes="(max-width: 570px) 100vw, 570px" /></figure>
</div>
<p>Say, you try to import <code>label_map_util</code> from the <code>utils</code> module when running TensorFlow’s <code>object_detection</code> API. You get the following error message:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="1,5" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> from utils import label_map_util
Traceback (most recent call last): File "&lt;pyshell#3>", line 1, in &lt;module> from utils import label_map_util
ModuleNotFoundError: No module named 'utils'</pre>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ac.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Question</strong>: How to fix the <code>ModuleNotFoundError: No module named 'utils'</code>?</p>
<h2>Solution Idea 1: Fix the Import Statement</h2>
<p>The most common source of the error is that you use the expression <code>from utils import &lt;something></code> but Python doesn’t find the utils module. You can fix this by replacing the import statement with the corrected <code>from object_detection.utils import &lt;something></code>. </p>
<p>For example, do not use these import statements:</p>
<pre class="wp-block-preformatted"><code>from <strong>utils</strong> import label_map_util
from <strong>utils</strong> import visualization_utils as vis_util</code></pre>
<p>Instead, use these import statements:</p>
<pre class="wp-block-preformatted"><code>from <strong>object_detection.utils</strong> import label_map_util
from <strong>object_detection.utils</strong> import visualization_utils as vis_util</code></pre>
<p>Everything remains the same except the <strong>bolded</strong> text. </p>
<p>This, of course, assumes that Python can resolve the <code>object_detection</code> API. You can follow the <a rel="noreferrer noopener" href="https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/" data-type="URL" data-id="https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/" target="_blank">installation recommendations here</a>, or if you already have TensorFlow installed, check out the Object Detection API installation tips <a rel="noreferrer noopener" href="https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/install.html#install-the-object-detection-api" data-type="URL" data-id="https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/install.html#install-the-object-detection-api" target="_blank">here</a>.</p>
<h2>Solution Idea 2: Modify System Path</h2>
<p>Another idea to solve this issue is to append the path of the <strong><em>TensorFlow Object Detection API</em></strong> folder to the system paths so your script can find it easily. </p>
<p>To do this, import the <code>sys</code> library and run <code>sys.path.append(my_path)</code> on the path to the <code>object_detection</code> folder that may reside in <code>/home/.../tensorflow/models/research/object_detection</code>, depending on your environment.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import sys
sys.path.append('path/to/object/detection/folder')</pre>
<h2>Solution Idea 3: Dirty Copy and Paste</h2>
<p>I don’t recommend using this approach but I still want to share it with you for comprehensibility. Try copying the <code>utils</code> folder from <code>models/research/object_detection</code> in the same directory as the Python file requiring <code>utils</code>.</p>
<h2>Solution Idea 4: Import Module from Another Folder (Utils)</h2>
<p>This is a better variant of the previous approach: use our in-depth guide to figure out a way to import the utils module correctly, even though it may reside on another path. This should usually do the trick.</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30e.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended Tutorial</strong>: <a rel="noreferrer noopener" href="https://blog.finxter.com/python-how-to-import-modules-from-another-folder/" data-type="post" data-id="19786" target="_blank">How to Import a Module from Another Path</a></p>
<p><strong>Resources</strong>: You can find more about this issue <a rel="noreferrer noopener" href="https://stackoverflow.com/questions/49545142/modulenotfounderror-no-module-named-utils" data-type="URL" data-id="https://stackoverflow.com/questions/49545142/modulenotfounderror-no-module-named-utils" target="_blank">here</a>, <a rel="noreferrer noopener" href="https://stackoverflow.com/questions/46494160/from-utils-import-label-map-util-import-error-no-module-named-utils" data-type="URL" data-id="https://stackoverflow.com/questions/46494160/from-utils-import-label-map-util-import-error-no-module-named-utils" target="_blank">here</a>, and <a rel="noreferrer noopener" href="https://www.tensorflow.org/hub/tutorials/object_detection" data-type="URL" data-id="https://www.tensorflow.org/hub/tutorials/object_detection" target="_blank">here</a>. Among other sources, these were also the ones that inspired the solutions provided in this tutorial.</p>
<p>Thanks for reading this—feel free to learn more about the benefits of a TensorFlow developer (we need to keep you motivated so you persist through the painful debugging process you’re currently in). <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f609.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30e.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended Tutorial</strong>: <a href="https://blog.finxter.com/tensorflow-developer-income-and-opportunity/" data-type="post" data-id="259596" target="_blank" rel="noreferrer noopener">TensorFlow Developer – Income and Opportunity</a></p>
</div>


https://www.sickgaming.net/blog/2022/11/25/tensorflow-modulenotfounderror-no-module-named-utils/