{"id":4729,"date":"2024-10-01T09:33:28","date_gmt":"2024-10-01T09:33:28","guid":{"rendered":"https:\/\/ubiq.co\/tech-blog\/?p=4729"},"modified":"2024-10-01T09:33:31","modified_gmt":"2024-10-01T09:33:31","slug":"what-does-if-__name__-__main__-do","status":"publish","type":"post","link":"https:\/\/ubiq.co\/tech-blog\/what-does-if-__name__-__main__-do\/","title":{"rendered":"What does if __name__ == &#8220;__main__&#8221;: do?"},"content":{"rendered":"\n<p>Python is a powerful programming language that offers tons of features and versatility. If you are a Python developer, then you may have frequently come across &#8216;__name__ == __main__&#8217; (also called name-main idiom) while reading other people&#8217;s code and wondered what it does. In this article, we will learn what this name-main idiom means, how this expression works and how to use it correctly.<\/p>\n\n\n\n<p>Before we understand what __name__=__main__ does, we need to separately learn how __name__ variable works and the difference between script and module in Python.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ubiq.co\/tech-blog\/what-does-if-__name__-__main__-do\/#Script_vs_Module\" >Script vs Module<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ubiq.co\/tech-blog\/what-does-if-__name__-__main__-do\/#How_does_name_variable_work\" >How does __name__ variable work<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ubiq.co\/tech-blog\/what-does-if-__name__-__main__-do\/#What_does_if_name_%E2%80%9C_main_%E2%80%9D_do\" >What does if __name__ == &#8220;__main__&#8221;: do?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/ubiq.co\/tech-blog\/what-does-if-__name__-__main__-do\/#Benefits_of_using_namemain\" >Benefits of using name==main<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/ubiq.co\/tech-blog\/what-does-if-__name__-__main__-do\/#When_to_use_name_equals_main\" >When to use name equals main<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/ubiq.co\/tech-blog\/what-does-if-__name__-__main__-do\/#When_not_to_use_name_equals_main\" >When not to use name equals main<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/ubiq.co\/tech-blog\/what-does-if-__name__-__main__-do\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Script_vs_Module\"><\/span>Script vs Module<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>A Python module is a self-contained file of variable definitions, classes, functions and commands that is meant for reuse. It is meant to be imported by other Python scripts and not to be executed on its own.<\/p>\n\n\n\n<p>A Python script is a file with a sequence of definitions, functions and commands. It is a standalone program meant to be executed on its own. It can, however, import other modules into it, for increased functionality.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_does_name_variable_work\"><\/span>How does __name__ variable work<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>__name__ is a special variable managed by Python during runtime. When a Python developer runs a script, the interpreter will automatically set __name__ variable to one of the following 2 values:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>It is set to __main__ if the script is run as a standalone program<\/li>\n\n\n\n<li>It is set to the name of the module, if the script is imported into other scripts<\/li>\n<\/ol>\n\n\n\n<p>Here is an example to demonstrate it. Let us say you create a Python file test.py with the following code which displays the value of __name__ variable.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(\"__name__ in test.py: \" + __name__)<\/pre>\n\n\n\n<p>Let us say you have another file app.py which imports the above file, using <a href=\"https:\/\/docs.python.org\/3\/reference\/import.html\" target=\"_blank\" rel=\"noreferrer noopener\">import<\/a> statement, and also prints __name__ variable&#8217;s value.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import test<br>print(\"__name__ in app.py:  \" + __name__)<\/pre>\n\n\n\n<p>When you run app.py file, it will print the value of __name__ variable twice, once from inside test.py and once from inside app.py.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ py app.py<br><br>__name__ in test.py:  test<br>__name__ in app.py:  __main__<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_does_if_name_%E2%80%9C_main_%E2%80%9D_do\"><\/span>What does if __name__ == &#8220;__main__&#8221;: do?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The name-main idiom works like any other if conditional expression in Python. If the value of __name__ variable is __main__ then all code nested under it will be executed. Else it will be skipped. As mentioned earlier, __name__ is a Python variable controlled by the interpreter. Its value is __main__ when you run the python script as a standalone script. Its value is equal to the name of the module if it is imported in other scripts.<\/p>\n\n\n\n<p>Here is a simple example test.py to demonstrate it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># test.py<br>def main():<br>    print('Hello World')<br><br>if __name__ == '__main__':<br>    main()<\/pre>\n\n\n\n<p>In the above code, we have defined main() function. But it is called only if name-main condition is true. In other words, it is called only if your script is executed as a standalone script, and not when it is imported elsewhere.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ python test.py<br>'Hello World'<\/pre>\n\n\n\n<p>On the other hand, if you import this code in other script, say test2.py, it will not call main() function.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># test2.py<br>import test.py<br>print('Bye Bye')<\/pre>\n\n\n\n<p>Let&#8217;s try running this code.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ python test2.py<br>Bye Bye<\/pre>\n\n\n\n<p>On the other hand, if you modify test.py to include some code outside the name==main condition, it will be executed. Here we have added a print statement outside the name-main condition.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># test.py<br>def main():<br>    print('Hello World')<br><br><strong>print('Good Morning')<\/strong><br><br>if __name__ == '__main__':<br>    main()<\/pre>\n\n\n\n<p>Now let us try running test2.py once again.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ python test2.py<br>Good Morning<br>Bye Bye<\/pre>\n\n\n\n<p>Name-main condition allows you to separate reusable parts of your code from the executable ones. Therefore, it also allows you to protect your code from being knowingly or unknowingly used by other scripts.<\/p>\n\n\n\n<p>Please note, you can always have multiple name-main blocks in your code.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># test.py<br>def main():<br>    print('Hello World')<br><br><strong>if __name__ == '__main__':<br>    print('Good evening')<\/strong><br><br>print('Good Morning')<br><br><strong>if __name__ == '__main__':<br>    main()<\/strong><\/pre>\n\n\n\n<p>When you run the script test2.py which imports the above test.py, you will get the following output.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ python test2.py<br>Good Morning<br>Bye Bye<\/pre>\n\n\n\n<p>As you can see above, both the name-main blocks in test.py were skipped during execution.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Benefits_of_using_namemain\"><\/span>Benefits of using name==main<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are several benefits of using name==main condition:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>It allows you to modularize code by clearly separating the executable and reusable parts of your code. The reusable parts of your code can be easily imported into other scripts. On the other hand, the executable code will be executed as standalone script. <\/li>\n\n\n\n<li>Name-Main idiom allows you to easily test specific parts of your code when it is executed on its own, without affecting other parts of the code.<\/li>\n\n\n\n<li>It also prevents unexpected code execution by skipping the part nested within __name__==__main__ condition, in case your code is imported in other scripts.<\/li>\n\n\n\n<li>It improves code readability by clearly separating executable and reusable code in your file.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"When_to_use_name_equals_main\"><\/span>When to use name equals main<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are several use cases when it is advisable to use name-main condition:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>One of the most common ways to use this name-main idiom is when you want your script to work as a standalone program as well as importable module.<\/li>\n\n\n\n<li>Another use case is when you want to prevent certain code from being executed when it is imported by other scripts. In this case, you can put that code in name-main idiom. For example, you may need to fetch user input from your file when you run it as a script but not when it is imported by other scripts.<\/li>\n\n\n\n<li>You can also use name-main idiom for unit testing by running your file from a separate module without running the code under name-main idiom. This will prevent unexpected errors.<\/li>\n\n\n\n<li>Alternatively, you can also include test code inside name-main condition and run it as a standalone script so that it does not run when the script is imported elsewhere.<\/li>\n\n\n\n<li>You can use it to demonstrate certain capabilities of the script which will not be executed when it is imported.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"When_not_to_use_name_equals_main\"><\/span>When not to use name equals main<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>On the flip side, there are a couple of use cases when it is not advisable to use name-main idiom:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>If your script is too large or complex, then it is better to create separate file for the module part and script part, instead of cramming everything into a single file<\/li>\n\n\n\n<li>If your Python script is meant to be run purely as a script and not as a module, then you do not need to include name-main condition.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this article, we have learnt what __name__==__main__ does, how it works, when to use, when not to use it. To summarize, the code included under name-main condition will be executed only when your Python file is run as a standalone script and not imported as a module. The rest of the code will be executed whether you run it as a standalone script or import it as a module in other files.<\/p>\n\n\n\n<p>Also read:<\/p>\n\n\n\n<p><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-element-is-hidden-in-jquery\/\">How to Check If Element is Hidden in jQuery<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/what-are-metaclasses-in-python\/\">What Are Metaclasses in Python<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/what-does-yield-keyword-do-in-python\/\">What Does Yield Keyword Do in Python<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Often python developers wonder what does __name__==&#8221;__main__&#8221; do. In this article, we will learn everything about name-main idiom.<\/p>\n","protected":false},"author":1,"featured_media":4757,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[301],"tags":[311],"class_list":["post-4729","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-namemain"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>What does if __name__ == &quot;__main__&quot;: do? - Ubiq BI<\/title>\n<meta name=\"description\" content=\"Often python developers wonder what does __name__==&quot;__main__&quot; do. In this article, we will learn everything about name-main idiom.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ubiq.co\/tech-blog\/what-does-if-__name__-__main__-do\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What does if __name__ == &quot;__main__&quot;: do? - Ubiq BI\" \/>\n<meta property=\"og:description\" content=\"Often python developers wonder what does __name__==&quot;__main__&quot; do. In this article, we will learn everything about name-main idiom.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ubiq.co\/tech-blog\/what-does-if-__name__-__main__-do\/\" \/>\n<meta property=\"og:site_name\" content=\"Ubiq BI\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ubiqbi\" \/>\n<meta property=\"article:published_time\" content=\"2024-10-01T09:33:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-01T09:33:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/10\/name-main-python.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"288\" \/>\n\t<meta property=\"og:image:height\" content=\"192\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sreeram Sreenivasan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:site\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sreeram Sreenivasan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/what-does-if-__name__-__main__-do\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/what-does-if-__name__-__main__-do\\\/\"},\"author\":{\"name\":\"Sreeram Sreenivasan\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"headline\":\"What does if __name__ == &#8220;__main__&#8221;: do?\",\"datePublished\":\"2024-10-01T09:33:28+00:00\",\"dateModified\":\"2024-10-01T09:33:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/what-does-if-__name__-__main__-do\\\/\"},\"wordCount\":1088,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/what-does-if-__name__-__main__-do\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/10\\\/name-main-python.jpg?fit=288%2C192&ssl=1\",\"keywords\":[\"name=main\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/what-does-if-__name__-__main__-do\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/what-does-if-__name__-__main__-do\\\/\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/what-does-if-__name__-__main__-do\\\/\",\"name\":\"What does if __name__ == \\\"__main__\\\": do? - Ubiq BI\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/what-does-if-__name__-__main__-do\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/what-does-if-__name__-__main__-do\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/10\\\/name-main-python.jpg?fit=288%2C192&ssl=1\",\"datePublished\":\"2024-10-01T09:33:28+00:00\",\"dateModified\":\"2024-10-01T09:33:31+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"description\":\"Often python developers wonder what does __name__==\\\"__main__\\\" do. In this article, we will learn everything about name-main idiom.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/what-does-if-__name__-__main__-do\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/what-does-if-__name__-__main__-do\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/what-does-if-__name__-__main__-do\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/10\\\/name-main-python.jpg?fit=288%2C192&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/10\\\/name-main-python.jpg?fit=288%2C192&ssl=1\",\"width\":288,\"height\":192,\"caption\":\"what does __name__==__main__ do\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/what-does-if-__name__-__main__-do\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"What does if __name__ == &#8220;__main__&#8221;: do?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\",\"name\":\"Ubiq BI\",\"description\":\"Build dashboards &amp; reports in minutes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\",\"name\":\"Sreeram Sreenivasan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"caption\":\"Sreeram Sreenivasan\"},\"description\":\"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/sreeram-sreenivasan\\\/\"],\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/author\\\/wordpress\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What does if __name__ == \"__main__\": do? - Ubiq BI","description":"Often python developers wonder what does __name__==\"__main__\" do. In this article, we will learn everything about name-main idiom.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/ubiq.co\/tech-blog\/what-does-if-__name__-__main__-do\/","og_locale":"en_US","og_type":"article","og_title":"What does if __name__ == \"__main__\": do? - Ubiq BI","og_description":"Often python developers wonder what does __name__==\"__main__\" do. In this article, we will learn everything about name-main idiom.","og_url":"https:\/\/ubiq.co\/tech-blog\/what-does-if-__name__-__main__-do\/","og_site_name":"Ubiq BI","article_publisher":"https:\/\/www.facebook.com\/ubiqbi","article_published_time":"2024-10-01T09:33:28+00:00","article_modified_time":"2024-10-01T09:33:31+00:00","og_image":[{"width":288,"height":192,"url":"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/10\/name-main-python.jpg","type":"image\/jpeg"}],"author":"Sreeram Sreenivasan","twitter_card":"summary_large_image","twitter_creator":"@UbiqBI","twitter_site":"@UbiqBI","twitter_misc":{"Written by":"Sreeram Sreenivasan","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ubiq.co\/tech-blog\/what-does-if-__name__-__main__-do\/#article","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/what-does-if-__name__-__main__-do\/"},"author":{"name":"Sreeram Sreenivasan","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"headline":"What does if __name__ == &#8220;__main__&#8221;: do?","datePublished":"2024-10-01T09:33:28+00:00","dateModified":"2024-10-01T09:33:31+00:00","mainEntityOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/what-does-if-__name__-__main__-do\/"},"wordCount":1088,"commentCount":0,"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/what-does-if-__name__-__main__-do\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/10\/name-main-python.jpg?fit=288%2C192&ssl=1","keywords":["name=main"],"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ubiq.co\/tech-blog\/what-does-if-__name__-__main__-do\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ubiq.co\/tech-blog\/what-does-if-__name__-__main__-do\/","url":"https:\/\/ubiq.co\/tech-blog\/what-does-if-__name__-__main__-do\/","name":"What does if __name__ == \"__main__\": do? - Ubiq BI","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/what-does-if-__name__-__main__-do\/#primaryimage"},"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/what-does-if-__name__-__main__-do\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/10\/name-main-python.jpg?fit=288%2C192&ssl=1","datePublished":"2024-10-01T09:33:28+00:00","dateModified":"2024-10-01T09:33:31+00:00","author":{"@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"description":"Often python developers wonder what does __name__==\"__main__\" do. In this article, we will learn everything about name-main idiom.","breadcrumb":{"@id":"https:\/\/ubiq.co\/tech-blog\/what-does-if-__name__-__main__-do\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ubiq.co\/tech-blog\/what-does-if-__name__-__main__-do\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ubiq.co\/tech-blog\/what-does-if-__name__-__main__-do\/#primaryimage","url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/10\/name-main-python.jpg?fit=288%2C192&ssl=1","contentUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/10\/name-main-python.jpg?fit=288%2C192&ssl=1","width":288,"height":192,"caption":"what does __name__==__main__ do"},{"@type":"BreadcrumbList","@id":"https:\/\/ubiq.co\/tech-blog\/what-does-if-__name__-__main__-do\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ubiq.co\/tech-blog\/"},{"@type":"ListItem","position":2,"name":"What does if __name__ == &#8220;__main__&#8221;: do?"}]},{"@type":"WebSite","@id":"https:\/\/ubiq.co\/tech-blog\/#website","url":"https:\/\/ubiq.co\/tech-blog\/","name":"Ubiq BI","description":"Build dashboards &amp; reports in minutes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc","name":"Sreeram Sreenivasan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","caption":"Sreeram Sreenivasan"},"description":"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.","sameAs":["https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/"],"url":"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/10\/name-main-python.jpg?fit=288%2C192&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pbGGTT-1eh","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/4729","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/comments?post=4729"}],"version-history":[{"count":36,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/4729\/revisions"}],"predecessor-version":[{"id":4766,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/4729\/revisions\/4766"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media\/4757"}],"wp:attachment":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media?parent=4729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/categories?post=4729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/tags?post=4729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}