{"id":1207,"date":"2019-01-09T19:40:19","date_gmt":"2019-01-09T19:40:19","guid":{"rendered":"http:\/\/www.afternerd.com\/blog\/?p=1207"},"modified":"2019-01-13T03:04:38","modified_gmt":"2019-01-13T03:04:38","slug":"python-__name__-__main__","status":"publish","type":"post","link":"https:\/\/www.afternerd.com\/blog\/python-__name__-__main__\/","title":{"rendered":"Python: __name__ == &#8220;__main__&#8221; Explained"},"content":{"rendered":"<p>You see it everywhere.<\/p>\n<p>If you have been coding in Python for a while, or just casually browsing Python Github repositories, you probably have come across this snippet of code.<\/p>\n<pre class=\"prettyprint\"><code>if __name__ == '__main__':\r\n  # do something<\/code><\/pre>\n<p>It is ubiquitous.<\/p>\n<p>In fact, this is how many times this above snippet appears in Github!<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"post-img alignnone wp-image-1230 size-full\" src=\"http:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2019\/01\/Screen-Shot-2019-01-09-at-10.20.40-AM.png\" alt=\"\" width=\"1046\" height=\"716\" srcset=\"https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2019\/01\/Screen-Shot-2019-01-09-at-10.20.40-AM.png 1046w, https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2019\/01\/Screen-Shot-2019-01-09-at-10.20.40-AM-300x205.png 300w, https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2019\/01\/Screen-Shot-2019-01-09-at-10.20.40-AM-768x526.png 768w, https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2019\/01\/Screen-Shot-2019-01-09-at-10.20.40-AM-1024x701.png 1024w, https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2019\/01\/Screen-Shot-2019-01-09-at-10.20.40-AM-200x137.png 200w, https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2019\/01\/Screen-Shot-2019-01-09-at-10.20.40-AM-400x274.png 400w, https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2019\/01\/Screen-Shot-2019-01-09-at-10.20.40-AM-600x411.png 600w, https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2019\/01\/Screen-Shot-2019-01-09-at-10.20.40-AM-800x548.png 800w\" sizes=\"(max-width: 1046px) 100vw, 1046px\" \/><\/p>\n<p><em>18 millions+ times!<\/em><\/p>\n<p>So without further ado, let&#8217;s study what this snippet of code does exactly.<\/p>\n<h2>What is __name__?<\/h2>\n<p><strong>__name__ <\/strong>is simply a built-in variable in Python which evaluates to the <em>name of the current module<\/em>.<\/p>\n<p>To fully understand what <strong>__name__<\/strong> is and how it used, let&#8217;s go through a series of examples.<\/p>\n<h3>Example 1<\/h3>\n<p>Let&#8217;s create a simple Python script in a file called <strong>test.py<\/strong><\/p>\n<pre class=\"prettyprint\"><code># test.py\r\nprint(\"__name__ in test.py is set to \" + __name__)<\/code><\/pre>\n<p>What this line does is that it is going to print out the variable <strong>__name__<\/strong> on the screen so that we can examine it.<\/p>\n<p>Let&#8217;s run this script from the terminal and see what we get.<\/p>\n<pre class=\"prettyprint\"><code>$ python test.py<\/code><\/pre>\n<p>Here is the output that you will see on the screen.<\/p>\n<pre class=\"prettyprint\"><code>$ python3 test.py\r\n__name__ in test.py is set to __main__<\/code><\/pre>\n<p>From this, we can conclude that in this example, the variable <strong>__name__<\/strong> was set to the string value <span class=\"symbol\">__main__<\/span><\/p>\n<p>Now let&#8217;s look at a slightly different example.<\/p>\n<h3>Example 2<\/h3>\n<p>Let&#8217;s create a new file <strong>test2.py\u00a0<\/strong>in the same directory as <strong>test.py<\/strong><\/p>\n<p>In this new file, let&#8217;s import <strong>test<\/strong> so that we can examine the <strong>__name__<\/strong> variable in <strong>test.py<\/strong> and let&#8217;s also print the <strong>__name__<\/strong> variable in <strong>test2.py<\/strong><\/p>\n<pre class=\"prettyprint\"><code># test2.py\r\nimport test\r\n\r\nprint(\"__name__ in test2.py is set to \" + __name__)<\/code><\/pre>\n<p>If you run the <strong>test2.py<\/strong> script from the terminal, this is what you will see on your screen.<\/p>\n<pre class=\"prettyprint\"><code>$ python3 test2.py\r\n__name__ in test.py is set to test\r\n__name__ in test2.py is set to __main__<\/code><\/pre>\n<p>Hmm interesting, so what is going on?<\/p>\n<p>Basically, what&#8217;s happening is that\u00a0<b>__name__<\/b>\u00a0 is set at the module level.<\/p>\n<p>It is set to the\u00a0<b>name<\/b>\u00a0of the module. In other words, for every module in your code, <strong>__name__<\/strong> will be set to that module name.<\/p>\n<p>And for the execution entry point, Python&#8217;s main script, the <strong>__name__<\/strong> variable will be set to <strong>__main__<\/strong><\/p>\n<p>That&#8217;s cool and all but how is this special variable used in practice?<\/p>\n<h2>How is __name__ used in real applications?<\/h2>\n<p>One reason for doing this is that if you want to write a module that can be executed directly or alternatively be imported and used in another module.<\/p>\n<p>For example, let&#8217;s create a dummy module <strong>hello.py<\/strong> that others can import to their own scripts, or they could directly execute it if they wish to.<\/p>\n<p>Here is what this module is supposed to do.<\/p>\n<p>If you directly execute the module, then it should print <span class=\"symbol\">Hello, Afternerd<\/span> on the screen.<\/p>\n<p>But if you import it instead, it sould provide you a function <span class=\"symbol\">hello()<\/span> where you can control who to greet.<\/p>\n<pre class=\"prettyprint\"><code># hello.py\r\ndef hello(name):\r\n  print(f\"Hello, {name}!\")\r\n\r\nif __name__ == '__main__':\r\n  hello('Afternerd')<\/code><\/pre>\n<p>Now let&#8217;s create a main script <strong>main.py<\/strong> that will import the <strong>hello<\/strong> module.<\/p>\n<pre class=\"prettyprint\"><code># main.py\r\nfrom hello import hello\r\n\r\nhello('Karim')<\/code><\/pre>\n<h3><em>What do you think will happen if we directly execute <strong>hello.py<\/strong>?<\/em><\/h3>\n<p>We just get <strong>Hello, Afternerd!<\/strong> printed on the screen since the if condition inside the <strong>hello<\/strong> module will be satisfied!<\/p>\n<pre class=\"prettyprint\"><code>$ python3 hello.py\r\nHello, Afternerd!<\/code><\/pre>\n<h3><em>How about if we directly execute <strong>main.py<\/strong> instead?<\/em><\/h3>\n<p>Now, we will only get <strong>Hello, Karim!<\/strong> printed on the screen.<\/p>\n<pre class=\"prettyprint\"><code>$ python3 main.py\r\nHello, Karim!<\/code><\/pre>\n<h3><em>But what if we hadn&#8217;t included the <span class=\"symbol\">if __name__ == &#8216;main&#8217;<\/span> condition in the <strong>hello<\/strong> module?<\/em><\/h3>\n<p>If you hadn&#8217;t included the if condition in your module, you would get this undesired output when you run the main script.<\/p>\n<pre class=\"prettyprint\"><code>$ python3 main.py\r\nHello, Afternerd!\r\nHello, Karim!<\/code><\/pre>\n<h3>Learning Python?<\/h3>\n<p>Check out <a href=\"https:\/\/courses.afternerd.com\/\">the Courses section!<\/a><\/p>\n\n<h2>Featured Posts<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/learn-python\/\">The Python Learning Path (From Beginner to Mastery)<\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/learn-computer-science\/\">Learn Computer Science (From Zero to Hero)<\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/coding-interview\/\">Coding Interview Preparation Guide<\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/stock-investing-for-beginners\/\">The Programmer&#8217;s Guide to Stock Market Investing<\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/start-programming-blog\/\">How to Start Your Programming Blog?<\/a><\/li>\n<\/ul>\n<div class=\"after-post-box\">\n<h2>Are you Beginning your Programming Career?<\/h2>\n<h3>I provide my best content for beginners in the newsletter.<\/h3>\n<ul>\n<li>Python tips for beginners, intermediate, and advanced levels.<\/li>\n<li>CS Career tips and advice.<\/li>\n<li>Special discounts on my premium courses when they launch.<\/li>\n<\/ul>\n<p>And so much more&#8230;<\/p>\n<h3>Subscribe now. It&#8217;s Free.<\/h3>\n<p><script type=\"text\/javascript\" src=\"\/\/mautic.afternerd.com\/form\/generate.js?id=2\"><\/script><\/p>\n<\/div>\n\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":1233,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[13],"yst_prominent_words":[1097,316,161,1105,1103,1098,380,721,1088,1085,1106,158,1083,1070,1073,237,677,1104,351,1084],"_links":{"self":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts\/1207"}],"collection":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/comments?post=1207"}],"version-history":[{"count":14,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts\/1207\/revisions"}],"predecessor-version":[{"id":1252,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts\/1207\/revisions\/1252"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/media\/1233"}],"wp:attachment":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/media?parent=1207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/categories?post=1207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/tags?post=1207"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=1207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}