{"id":575,"date":"2020-10-16T02:05:41","date_gmt":"2020-10-16T02:05:41","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=575"},"modified":"2025-03-30T10:08:03","modified_gmt":"2025-03-30T10:08:03","slug":"python-__name__","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-basics\/python-__name__\/","title":{"rendered":"Python __name__"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the Python <code>__name__<\/code> variable and how to use it effectively in modules.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='whats-python-__name__'>What&#8217;s Python __name__? <a href=\"#whats-python-__name__\" class=\"anchor\" id=\"whats-python-__name__\" title=\"Anchor for What&#039;s Python __name__?\">#<\/a><\/h2>\n\n\n\n<p>If you have gone through Python code, you&#8217;ve likely seen the <code>__name__<\/code> variable like the following:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    main()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And you might wonder what the <code>__name__<\/code> variable is.<\/p>\n\n\n\n<p>Since the <code>__name__<\/code> variable has double underscores at both sides, it&#8217;s called a <strong>dunder name<\/strong>. The dunder stands for <strong>d<\/strong>ouble <strong>under<\/strong>scores<\/p>\n\n\n\n<p>The <code>__name__<\/code> is a special variable in Python. It&#8217;s special because Python assigns a different value to it depending on how its containing script executes.<\/p>\n\n\n\n<p>When you import a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-module\/\">module<\/a>, Python executes the file associated with the module.<\/p>\n\n\n\n<p>Often, you want to write a script that can be executed directly or imported as a module. The <code>__name__<\/code> variable allows you to do that.<\/p>\n\n\n\n<p>When you run the script directly, Python sets the <code>__name__<\/code> variable to <code>'__main__'<\/code>. <\/p>\n\n\n\n<p>However, if you import a file as a module, Python sets the module name to the <code>__name__<\/code> variable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='python-__name__-variable-example'>Python __name__ variable example <a href=\"#python-__name__-variable-example\" class=\"anchor\" id=\"python-__name__-variable-example\" title=\"Anchor for Python __name__ variable example\">#<\/a><\/h2>\n\n\n\n<p>First, create a new module called <code>billing<\/code> that has two functions: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>calculate_tax()<\/code> <\/li>\n\n\n\n<li><code>print_billing_doc()<\/code><\/li>\n<\/ul>\n\n\n\n<p>Additionally, add a statement that prints out the <code>__name__<\/code> variable to the screen:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">calculate_tax<\/span><span class=\"hljs-params\">(price, tax)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> price * tax\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">print_billing_doc<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    tax_rate = <span class=\"hljs-number\">0.1<\/span>\n    products = &#91;{<span class=\"hljs-string\">'name'<\/span>: <span class=\"hljs-string\">'Book'<\/span>,  <span class=\"hljs-string\">'price'<\/span>: <span class=\"hljs-number\">30<\/span>},\n                {<span class=\"hljs-string\">'name'<\/span>: <span class=\"hljs-string\">'Pen'<\/span>, <span class=\"hljs-string\">'price'<\/span>: <span class=\"hljs-number\">5<\/span>}]\n\n    <span class=\"hljs-comment\"># print billing header<\/span>\n    print(<span class=\"hljs-string\">f'Name\\tPrice\\tTax'<\/span>)\n\n    <span class=\"hljs-comment\"># print the billing item<\/span>\n    <span class=\"hljs-keyword\">for<\/span> product <span class=\"hljs-keyword\">in<\/span> products:\n        tax = calculate_tax(product&#91;<span class=\"hljs-string\">'price'<\/span>], tax_rate)\n        print(<span class=\"hljs-string\">f\"<span class=\"hljs-subst\">{product&#91;<span class=\"hljs-string\">'name'<\/span>]}<\/span>\\t<span class=\"hljs-subst\">{product&#91;<span class=\"hljs-string\">'price'<\/span>]}<\/span>\\t<span class=\"hljs-subst\">{tax}<\/span>\"<\/span>)\n\n\nprint(__name__)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, create a new file called <code>app.py<\/code> and import the <code>billing<\/code> module:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> billing<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When you execute the <code>app.py<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">python<\/span> <span class=\"hljs-selector-tag\">app<\/span><span class=\"hljs-selector-class\">.py<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p> &#8230;the <code>__name__<\/code> variable shows the following value:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">billing<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It means that Python does execute the <code>billing.py<\/code> file when you import the billing module to the <code>app.py<\/code> file.<\/p>\n\n\n\n<p>The <code>__name__<\/code> variable in the <code>app.py<\/code> set to the module name which is <code>billing<\/code>.<\/p>\n\n\n\n<p>If you execute the <code>billing.py<\/code> as a script directly:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">python<\/span> <span class=\"hljs-selector-tag\">billing<\/span><span class=\"hljs-selector-class\">.py<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>&#8230; you&#8217;ll see the following output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">__main__<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this case the value of the <code>__name__<\/code> variable is <code>'__main__'<\/code> inside the <code>billing.py<\/code>.<\/p>\n\n\n<div class=\"wp-block-image is-resized\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"498\" height=\"298\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-__name__.png\" alt=\"Python __name__\" class=\"wp-image-578\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-__name__.png 498w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-__name__-300x180.png 300w\" sizes=\"auto, (max-width: 498px) 100vw, 498px\" \/><\/figure>\n<\/div>\n\n\n<p>Therefore, the <code>__name__<\/code> variable allows you to check when the file is executed directly or imported as a module.<\/p>\n\n\n\n<p>For example, to execute the <code>print_billing_doc()<\/code> function when the <code>billing.py<\/code> executes directly as a script, you can add the following statement to the <code>billing.py<\/code> module:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    print_billing_doc()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, execute the <code>billing.py<\/code> as a script, you&#8217;ll see the following output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Name    Price   Tax\nBook    <span class=\"hljs-number\">30<\/span>      <span class=\"hljs-number\">3.0<\/span>\nPen     <span class=\"hljs-number\">5<\/span>       <span class=\"hljs-number\">0.5<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>However, when you execute the <code>app.py<\/code>, you won&#8217;t see the <code>if<\/code> block executed because the <code>__name__<\/code> variable doesn&#8217;t set to the <code>'__main__'<\/code> but <code>'billing'<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python assign the <code>'__main__'<\/code> to the <code>__name__<\/code> variable when you run the script directly and the module name if you import the script as a module.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=__name__\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"575\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-__name__\/\"\n\t\t\t\tdata-post-title=\"Python __name__\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"575\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-__name__\/\"\n\t\t\t\tdata-post-title=\"Python __name__\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about the Python __name__ variable and how to use it effectively in modules<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":37,"menu_order":59,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-575","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/575","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=575"}],"version-history":[{"count":3,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/575\/revisions"}],"predecessor-version":[{"id":7216,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/575\/revisions\/7216"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/37"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}