{"id":637,"date":"2023-03-21T16:03:17","date_gmt":"2023-03-21T16:03:17","guid":{"rendered":"https:\/\/tinyytopic.com\/?p=637"},"modified":"2023-03-21T16:00:22","modified_gmt":"2023-03-21T16:00:22","slug":"what-does-if-__name__-__main__-do-in-python","status":"publish","type":"post","link":"https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/what-does-if-__name__-__main__-do-in-python\/","title":{"rendered":"What does if __name__ == &#8220;__main__&#8221;: do in Python?"},"content":{"rendered":"\n<p style=\"font-size:15px\"><br>The line <code>if __name__ == \"__main__\":<\/code> in Python is used to check whether the current module is being run as the main program or it is being imported as a module into some other program. This is a common technique used in Python to write code that can be used both as a standalone program and as a module.<\/p>\n\n\n\n<p style=\"font-size:15px\"><br>When a Python module is imported into another module, all of the code in the imported module is executed, including any function or class definitions, global variables, and other statements. However, sometimes you may want to write code in a module that should only be executed if the module is run as the main program, and not if it is imported as a module into some other program.<\/p>\n\n\n\n<p style=\"font-size:15px\"><br>The <code>__name__<\/code> variable in Python is a special variable that is automatically set by the Python interpreter. When a module is run as the main program, <code>__name__<\/code> is set to <code>\"__main__\"<\/code>. When a module is imported as a module into another program, <code>__name__<\/code> is set to the name of the module.<\/p>\n\n\n\n<p style=\"font-size:15px\"><br>Therefore, the line <code>if __name__ == \"__main__\":<\/code> allows you to specify which parts of the code should only be executed if the module is run as the main program. Any code that comes after this line will only be executed if the module is run as the main program, and not if it is imported as a module into another program.<\/p>\n\n\n\n<p style=\"font-size:15px\"><br>Here&#8217;s a simple Python program that uses <code>if __name__ == \"__main__\":<\/code> to print a message only if the module is run as the main program:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"atomic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def hello_world():\n    print(\"Hello, World!\")\n\nif __name__ == \"__main__\":\n    hello_world()<\/pre>\n\n\n\n<p style=\"font-size:15px\">In this example, we define a function called <code>hello_world()<\/code> that prints the message &#8220;Hello, World!&#8221; to the console.<\/p>\n\n\n\n<p style=\"font-size:15px\"><br>Then, we use the <code>if __name__ == \"__main__\":<\/code> statement to call the <code>hello_world()<\/code> function only if the module is being run as the main program. If the module is being imported into another program, the <code>hello_world()<\/code> function will not be called.<\/p>\n\n\n\n<p style=\"font-size:15px\"><br>You can save this code to a file called <code>hello.py<\/code>, and then run it from the command line using the <code>python<\/code> command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ python hello.py\r\nHello, World!<\/code><\/pre>\n\n\n\n<p style=\"font-size:15px\">Alternatively, you can import this module into another Python program, and the <code>hello_world()<\/code> function will not be called:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import hello\r\n\r\n# This code does not call the hello_world() function because it was not run as the main program\r<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The line if __name__ == &#8220;__main__&#8221;: in Python is used to check whether the current module is being run as the main program or it is being imported as a module into some other program. This is a common technique used in Python to write code that can be used both as a standalone program [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_uag_custom_page_level_css":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[42,12],"tags":[14,13,16,80,15],"class_list":["post-637","post","type-post","status-publish","format-standard","hentry","category-general-qa","category-python","tag-programming-language","tag-python","tag-python-code","tag-python-qna","tag-python-sample-code"],"aioseo_notices":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>What does if __name__ == &quot;__main__&quot;: do in Python? - tinyytopic.com<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/what-does-if-__name__-__main__-do-in-python\/\" \/>\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 in Python? - tinyytopic.com\" \/>\n<meta property=\"og:description\" content=\"The line if __name__ == &quot;__main__&quot;: in Python is used to check whether the current module is being run as the main program or it is being imported as a module into some other program. This is a common technique used in Python to write code that can be used both as a standalone program [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/what-does-if-__name__-__main__-do-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"tinyytopic.com\" \/>\n<meta property=\"article:published_time\" content=\"2023-03-21T16:03:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-03-21T16:00:22+00:00\" \/>\n<meta name=\"author\" content=\"tinyytopic.com\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"tinyytopic.com\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/what-does-if-__name__-__main__-do-in-python\/\",\"url\":\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/what-does-if-__name__-__main__-do-in-python\/\",\"name\":\"What does if __name__ == \\\"__main__\\\": do in Python? - tinyytopic.com\",\"isPartOf\":{\"@id\":\"https:\/\/tinyytopic.com\/#website\"},\"datePublished\":\"2023-03-21T16:03:17+00:00\",\"dateModified\":\"2023-03-21T16:00:22+00:00\",\"author\":{\"@id\":\"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb\"},\"breadcrumb\":{\"@id\":\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/what-does-if-__name__-__main__-do-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/what-does-if-__name__-__main__-do-in-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/what-does-if-__name__-__main__-do-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/tinyytopic.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"What does if __name__ == &#8220;__main__&#8221;: do in Python?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/tinyytopic.com\/#website\",\"url\":\"https:\/\/tinyytopic.com\/\",\"name\":\"tinyytopic.com\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/tinyytopic.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb\",\"name\":\"tinyytopic.com\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/tinyytopic.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g\",\"caption\":\"tinyytopic.com\"},\"sameAs\":[\"http:\/\/tinyytopic.com\"],\"url\":\"https:\/\/tinyytopic.com\/index.php\/author\/mmkmuthukumar21gmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What does if __name__ == \"__main__\": do in Python? - tinyytopic.com","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:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/what-does-if-__name__-__main__-do-in-python\/","og_locale":"en_US","og_type":"article","og_title":"What does if __name__ == \"__main__\": do in Python? - tinyytopic.com","og_description":"The line if __name__ == \"__main__\": in Python is used to check whether the current module is being run as the main program or it is being imported as a module into some other program. This is a common technique used in Python to write code that can be used both as a standalone program [&hellip;]","og_url":"https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/what-does-if-__name__-__main__-do-in-python\/","og_site_name":"tinyytopic.com","article_published_time":"2023-03-21T16:03:17+00:00","article_modified_time":"2023-03-21T16:00:22+00:00","author":"tinyytopic.com","twitter_card":"summary_large_image","twitter_misc":{"Written by":"tinyytopic.com","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/what-does-if-__name__-__main__-do-in-python\/","url":"https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/what-does-if-__name__-__main__-do-in-python\/","name":"What does if __name__ == \"__main__\": do in Python? - tinyytopic.com","isPartOf":{"@id":"https:\/\/tinyytopic.com\/#website"},"datePublished":"2023-03-21T16:03:17+00:00","dateModified":"2023-03-21T16:00:22+00:00","author":{"@id":"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb"},"breadcrumb":{"@id":"https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/what-does-if-__name__-__main__-do-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/what-does-if-__name__-__main__-do-in-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/what-does-if-__name__-__main__-do-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/tinyytopic.com\/"},{"@type":"ListItem","position":2,"name":"What does if __name__ == &#8220;__main__&#8221;: do in Python?"}]},{"@type":"WebSite","@id":"https:\/\/tinyytopic.com\/#website","url":"https:\/\/tinyytopic.com\/","name":"tinyytopic.com","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/tinyytopic.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb","name":"tinyytopic.com","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/tinyytopic.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g","caption":"tinyytopic.com"},"sameAs":["http:\/\/tinyytopic.com"],"url":"https:\/\/tinyytopic.com\/index.php\/author\/mmkmuthukumar21gmail-com\/"}]}},"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"tinyytopic.com","author_link":"https:\/\/tinyytopic.com\/index.php\/author\/mmkmuthukumar21gmail-com\/"},"uagb_comment_info":0,"uagb_excerpt":"The line if __name__ == \"__main__\": in Python is used to check whether the current module is being run as the main program or it is being imported as a module into some other program. This is a common technique used in Python to write code that can be used both as a standalone program&hellip;","_links":{"self":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts\/637","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/comments?post=637"}],"version-history":[{"count":4,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts\/637\/revisions"}],"predecessor-version":[{"id":641,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts\/637\/revisions\/641"}],"wp:attachment":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/media?parent=637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/categories?post=637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/tags?post=637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}