π·οΈ __name__
__name__ is a special variable that tells Python the name under which a file (module) is being used.
The most important thing to understand is that its value changes depending on how the Python file is run.
βΆοΈ When You Run the File Directly
Suppose we have a file called calculator.py:
print(__name__)If you run:
python calculator.pyPython sets:
__name__ = "__main__"So the output is:
__main__π The file being run directly is always called "__main__" by Python.
π¦ When You Import the File
Suppose calculator.py contains:
print(__name__)And another file contains:
import calculatorNow calculator.py is not being run directly. It is being imported.
So Python sets:
__name__ = "calculator"Output:
calculatorπ Why if __name__ == "__main__"?
Consider:
# calculator.py
def add(a, b):
return a + b
if __name__ == "__main__":
print(add(10, 5)) #15If we import calculator.py:
import calculatorNow:
__name__ == "calculator" is Falseprint(add(10, 5)) does NOT run
But we can still use:
print(calculator.add(10, 5))π§ Think of It Like This

Therefore:
if __name__ == "__main__":means:
"Run this code only if this file is being executed directly, not when it is imported."
β Remember
__name__ β Tells Python how the current file is being used.
"__main__" β The file is being run directly.
<div class="infographic-pyname">
<style>
.infographic-pyname {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-soft:#e3ebfd;
--line: #ddd6c9;
--true-c: #1f9d55; --true-soft: #e3f5ec;
--false-c: #c0392b; --false-soft: #fbe9e7;
--mono-font: 'IBM Plex Mono', 'SFMono-Regular', Menlo, monospace;
--display-font: 'Fraunces', Georgia, serif;
--body-font: 'Inter', system-ui, sans-serif;
box-sizing: border-box;
max-width: 720px;
margin: 0 auto;
font-family: var(--body-font);
color: var(--ink);
background: var(--paper);
}
.infographic-pyname[data-theme="dark"] {
--ink: #e8e6e1;
--ink-soft: #9a968c;
--paper: #121212;
--paper-alt: #1e1e26;
--accent: #5b8def;
--accent-soft:#202942;
--line: #2c313d;
--true-c: #6fdc8c; --true-soft: #1c3324;
--false-c: #e57373; --false-soft: #3a2222;
}
.infographic-pyname *, .infographic-pyname *::before, .infographic-pyname *::after { box-sizing: border-box; }
.infographic-pyname .nm-wrap { padding: 0 clamp(1rem, 4vw, 2rem) 1.5rem; }
.infographic-pyname h1 {
font-family: var(--display-font); font-weight: 600;
font-size: clamp(1.5rem, 5vw, 2.05rem); text-align: center; margin: 1.5rem 0 0.25rem; letter-spacing: -0.01em;
}
.infographic-pyname .nm-sub { text-align: center; color: var(--ink-soft); font-size: 0.86rem; margin: 0 0 1.3rem; }
.infographic-pyname .files-row { display: grid; grid-template-columns: 1fr 1fr; gap: 0.8rem; margin-bottom: 1.2rem; }
@media (max-width: 540px) { .infographic-pyname .files-row { grid-template-columns: 1fr; } }
.infographic-pyname .file-card { border: 1.5px solid var(--line); border-radius: 8px; overflow: hidden; transition: border-color 0.25s, box-shadow 0.25s; }
.infographic-pyname .file-card.is-focus { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.infographic-pyname .file-bar {
font-family: var(--mono-font); font-size: 0.75rem; font-weight: 700; color: var(--ink-soft);
background: var(--paper-alt); padding: 0.4rem 0.6rem; border-bottom: 1px solid var(--line);
}
.infographic-pyname .file-body {
font-family: var(--mono-font); font-size: 0.76rem; line-height: 1.65; white-space: pre;
padding: 0.6rem 0.7rem; background: var(--paper); color: var(--ink); min-height: 5.5em;
}
.infographic-pyname .file-body .kw { color: var(--accent); font-weight: 700; }
.infographic-pyname .file-body .hl { background: var(--accent-soft); border-radius: 3px; }
.infographic-pyname .mode-row { display: flex; justify-content: center; gap: 0.5rem; margin-bottom: 1rem; flex-wrap: wrap; }
.infographic-pyname .mode-btn {
font: inherit; font-family: var(--mono-font); font-size: 0.8rem; font-weight: 700;
background: var(--paper-alt); border: 1.5px solid var(--line); color: var(--ink-soft);
padding: 0.45rem 0.75rem; cursor: pointer; border-radius: 6px;
}
.infographic-pyname .mode-btn.is-selected { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.infographic-pyname .run-row { display: flex; justify-content: center; margin-bottom: 1rem; }
.infographic-pyname .run-btn {
font: inherit; font-family: var(--mono-font); font-size: 0.85rem; font-weight: 700;
background: var(--accent); color: #fff; border: none; border-radius: 999px;
padding: 0.55rem 1.2rem; cursor: pointer;
}
.infographic-pyname .run-btn:disabled { opacity: 0.5; cursor: default; }
.infographic-pyname .name-badge-row { display: flex; justify-content: center; gap: 0.6rem; margin-bottom: 1rem; flex-wrap: wrap; }
.infographic-pyname .status-card { background: var(--paper-alt); border: 1px solid var(--line); border-radius: 6px; padding: 0.5rem 0.8rem; text-align: center; font-family: var(--mono-font); font-size: 0.82rem; }
.infographic-pyname .status-card .badge { font-weight: 700; padding: 0.08rem 0.45rem; border-radius: 999px; margin-left: 0.3rem; }
.infographic-pyname .status-card .badge.t { background: var(--true-soft); color: var(--true-c); }
.infographic-pyname .status-card .badge.f { background: var(--false-soft); color: var(--false-c); }
.infographic-pyname .device {
--ed-bg: #1e1e1e; --ed-bar: #2d2d2d; --ed-text: #d4d4d4; --ed-soft: #8a8a8a; --ed-ok: #6fdc8c;
background: var(--ed-bg); border: 1px solid var(--line); border-radius: 8px; overflow: hidden;
}
.infographic-pyname .device-bar {
display: flex; align-items: center; gap: 0.4rem;
background: var(--ed-bar); padding: 0.5rem 0.7rem; border-bottom: 1px solid #3a3a3a;
font-size: 0.72rem; color: var(--ed-soft); font-weight: 600;
}
.infographic-pyname .device-bar .dot { width: 9px; height: 9px; border-radius: 50%; background: #5a5a5a; }
.infographic-pyname .device-bar .dot:nth-child(1) { background: #ff5f56; }
.infographic-pyname .device-body { padding: 0.75rem 0.9rem; font-family: var(--mono-font); font-size: 0.8rem; color: var(--ed-text); min-height: 90px; }
.infographic-pyname .log-line { display: flex; gap: 0.5rem; line-height: 1.75; opacity: 0; animation: nm-fadein 0.25s ease forwards; flex-wrap: wrap; }
@keyframes nm-fadein { from { opacity: 0; transform: translateY(3px); } to { opacity: 1; transform: translateY(0); } }
.infographic-pyname .log-line .out { color: #fff; }
.infographic-pyname .log-line .fin { color: var(--ed-ok); font-weight: 700; }
.infographic-pyname .log-empty { color: var(--ed-soft); }
.infographic-pyname .nm-caption { text-align: center; font-size: 0.85rem; color: var(--ink-soft); margin-top: 0.9rem; }
@media (prefers-reduced-motion: reduce) {
.infographic-pyname *, .infographic-pyname *::before, .infographic-pyname *::after {
animation-duration: 0.001ms !important; transition-duration: 0.001ms !important;
}
}
</style>
<div class="nm-wrap">
<h1>__name__</h1>
<p class="nm-sub">Same file, same code β but __name__ changes depending on how it's launched.</p>
<div class="files-row">
<div class="file-card" id="cardCalc">
<div class="file-bar">calculator.py</div>
<div class="file-body"><span class="kw">def</span> add(a, b):
<span class="kw">return</span> a + b
<span class="kw">if</span> <span class="hl" id="hlIf">__name__ == "__main__"</span>:
print(add(10, 5))</div>
</div>
<div class="file-card" id="cardApp">
<div class="file-bar">app.py</div>
<div class="file-body"><span class="kw">import</span> calculator
print(calculator.add(10, 5))</div>
</div>
</div>
<div class="mode-row">
<button class="mode-btn is-selected" data-mode="direct">python calculator.py</button>
<button class="mode-btn" data-mode="import">python app.py</button>
</div>
<div class="run-row"><button class="run-btn" id="runBtn">Run</button></div>
<div class="name-badge-row">
<div class="status-card">calculator.py __name__ = <span class="badge t" id="nameBadge">"__main__"</span></div>
<div class="status-card">if block runs? <span class="badge t" id="ifBadge">Yes</span></div>
</div>
<div class="device">
<div class="device-bar"><span class="dot"></span><span class="dot"></span><span class="dot"></span> Terminal</div>
<div class="device-body" id="nmLog"><div class="log-empty">Pick how it's run, then hit Run.</div></div>
</div>
<p class="nm-caption" id="nmCaption">Run the file directly and __name__ becomes "__main__" β the if block fires.</p>
</div>
<script>
(function () {
var root = document.currentScript.closest('.infographic-pyname')
|| document.querySelector('.infographic-pyname');
var bodyAttr = document.body.getAttribute('data-darkmode');
var dark;
if (bodyAttr === 'true') { dark = true; }
else if (bodyAttr === 'false') { dark = false; }
else {
var match = document.cookie.match(/(?:^|;\s*)darkMode=([^;]+)/);
dark = match ? match[1] === 'true' : false;
}
if (dark) root.setAttribute('data-theme', 'dark');
var modeBtns = root.querySelectorAll('.mode-btn');
var runBtn = root.querySelector('#runBtn');
var nameBadge = root.querySelector('#nameBadge');
var ifBadge = root.querySelector('#ifBadge');
var nmLog = root.querySelector('#nmLog');
var nmCaption = root.querySelector('#nmCaption');
var cardCalc = root.querySelector('#cardCalc');
var cardApp = root.querySelector('#cardApp');
var currentMode = 'direct';
var firstLog = true;
var captions = {
direct: 'Run the file directly and __name__ becomes "__main__" β the if block fires.',
import: 'Import the file instead and __name__ becomes "calculator" β the if block is skipped.'
};
modeBtns.forEach(function (btn) {
btn.addEventListener('click', function () {
modeBtns.forEach(function (b) { b.classList.toggle('is-selected', b === btn); });
currentMode = btn.dataset.mode;
nmCaption.textContent = captions[currentMode];
});
});
function logLine(text, cls) {
if (firstLog) { nmLog.innerHTML = ''; firstLog = false; }
var line = document.createElement('div');
line.className = 'log-line';
line.innerHTML = '<span class="' + (cls || 'out') + '">' + text + '</span>';
nmLog.appendChild(line);
nmLog.scrollTop = nmLog.scrollHeight;
}
function setBadge(el, text, isTrue) {
el.textContent = text;
el.className = 'badge ' + (isTrue ? 't' : 'f');
}
runBtn.addEventListener('click', function () {
runBtn.disabled = true;
firstLog = true;
nmLog.innerHTML = '';
cardCalc.classList.remove('is-focus');
cardApp.classList.remove('is-focus');
var steps = [];
if (currentMode === 'direct') {
steps.push(function () {
cardCalc.classList.add('is-focus');
logLine('$ python calculator.py');
});
steps.push(function () {
logLine('calculator.py runs top to bottom β Python sets __name__ = "__main__"');
setBadge(nameBadge, '"__main__"', true);
});
steps.push(function () {
logLine('if __name__ == "__main__": β True', 'fin');
setBadge(ifBadge, 'Yes', true);
});
steps.push(function () {
logLine('15');
});
} else {
steps.push(function () {
cardApp.classList.add('is-focus');
logLine('$ python app.py');
});
steps.push(function () {
logLine('app.py hits "import calculator" β calculator.py runs top to bottom too');
});
steps.push(function () {
cardCalc.classList.add('is-focus');
cardApp.classList.remove('is-focus');
logLine('calculator.py is being imported β Python sets __name__ = "calculator"');
setBadge(nameBadge, '"calculator"', false);
});
steps.push(function () {
logLine('if __name__ == "__main__": β False, skipped', 'fin');
setBadge(ifBadge, 'No', false);
});
steps.push(function () {
cardApp.classList.add('is-focus');
logLine('app.py explicitly calls calculator.add(10, 5)');
});
steps.push(function () {
logLine('15');
});
}
var i = 0;
function next() {
if (i < steps.length) {
steps[i]();
i++;
setTimeout(next, 550);
} else {
runBtn.disabled = false;
}
}
next();
});
})();
</script>
</div>