{"id":130050,"date":"2022-11-25T19:36:59","date_gmt":"2022-11-25T19:36:59","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=924941"},"modified":"2022-11-25T19:36:59","modified_gmt":"2022-11-25T19:36:59","slug":"tensorflow-modulenotfounderror-no-module-named-utils","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/11\/25\/tensorflow-modulenotfounderror-no-module-named-utils\/","title":{"rendered":"TensorFlow ModuleNotFoundError: No Module Named \u2018utils\u2019"},"content":{"rendered":"\n<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;}'>\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<h2>Problem Formulation<\/h2>\n<div class=\"wp-block-image\">\n<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=\"auto, (max-width: 570px) 100vw, 570px\" \/><\/figure>\n<\/div>\n<p>Say, you try to import <code>label_map_util<\/code> from the <code>utils<\/code> module when running TensorFlow&#8217;s <code>object_detection<\/code> API. You get the following error message:<\/p>\n<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\nTraceback (most recent call last): File \"&lt;pyshell#3>\", line 1, in &lt;module> from utils import label_map_util\nModuleNotFoundError: No module named 'utils'<\/pre>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4ac.png\" alt=\"\ud83d\udcac\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Question<\/strong>: How to fix the <code>ModuleNotFoundError: No module named 'utils'<\/code>?<\/p>\n<h2>Solution Idea 1: Fix the Import Statement<\/h2>\n<p>The most common source of the error is that you use the expression <code>from utils import &lt;something><\/code> but Python doesn&#8217;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>\n<p>For example, do not use these import statements:<\/p>\n<pre class=\"wp-block-preformatted\"><code>from <strong>utils<\/strong> import label_map_util\nfrom <strong>utils<\/strong> import visualization_utils as vis_util<\/code><\/pre>\n<p>Instead, use these import statements:<\/p>\n<pre class=\"wp-block-preformatted\"><code>from <strong>object_detection.utils<\/strong> import label_map_util\nfrom <strong>object_detection.utils<\/strong> import visualization_utils as vis_util<\/code><\/pre>\n<p>Everything remains the same except the <strong>bolded<\/strong> text. <\/p>\n<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>\n<h2>Solution Idea 2: Modify System Path<\/h2>\n<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>\n<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>\n<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\nsys.path.append('path\/to\/object\/detection\/folder')<\/pre>\n<h2>Solution Idea 3: Dirty Copy and Paste<\/h2>\n<p>I don&#8217;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>\n<h2>Solution Idea 4: Import Module from Another Folder (Utils)<\/h2>\n<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>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30e.png\" alt=\"\ud83c\udf0e\" 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>\n<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>\n<p>Thanks for reading this&#8212;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&#8217;re currently in). <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f609.png\" alt=\"\ud83d\ude09\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30e.png\" alt=\"\ud83c\udf0e\" 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 &#8211; Income and Opportunity<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Problem Formulation Say, you try to import label_map_util from the utils module when running TensorFlow&#8217;s object_detection API. You get the following error message: >>> from utils import label_map_util Traceback (most recent call last): File &#8220;&lt;pyshell#3>&#8221;, line 1, in &lt;module> from utils import label_map_util ModuleNotFoundError: No module named &#8216;utils&#8217; Question: How to [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-130050","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/130050","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=130050"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/130050\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=130050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=130050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=130050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}