From f52255f7d19227548c82de2679b311ea1e817abc Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Thu, 5 Mar 2026 18:48:21 -0500 Subject: [PATCH 1/3] if __name__ == '__main__': --- .../guides/python-guides/if-name-main.md | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 pydis_site/apps/content/resources/guides/python-guides/if-name-main.md diff --git a/pydis_site/apps/content/resources/guides/python-guides/if-name-main.md b/pydis_site/apps/content/resources/guides/python-guides/if-name-main.md new file mode 100644 index 000000000..3509b9f20 --- /dev/null +++ b/pydis_site/apps/content/resources/guides/python-guides/if-name-main.md @@ -0,0 +1,60 @@ +--- +title: "`if __name__ == "__main__"`" +description: "What is `if __name__ == "__main__"`, and why would you use it?" +--- + +You might see this structure in a Python file: + +```py +# prog.py + +def main_fn(): + # Put the main part of your program here. + +if __name__ == "__main__": + main_fn() +``` + +If the file is run directly (with `python prog.py` or `py prog.py`), it's called the main file of your program. In that case, our `main_fn()` function will be run. If instead the file is imported by another file, then `main_fn()` will not be run. + +When Python runs your module (your file), it automatically sets the `__name__` special global variable to `"__main__"`. The other way to run your file is to import it in a larger program. In that case, `__name__` is instead set to the filename of your module minus the `.py` extension. + + +## Another Example + +```py +# foo.py + +print("spam") + +if __name__ == "__main__": + print("eggs") +``` + +If you run the above module `foo.py` directly, `__name__` will be equal to `"__main__"`, so both `spam`and `eggs` will be printed. Now consider this next example: + +```py +# bar.py + +import foo +``` + +If you run this file, it will import foo, which executes the code in `foo.py`. Now in foo.py `__name__` will be equal to `"foo"`. First it will print `spam`, and then the `if` statement will be false, so `eggs` won't print. + +Often the code in the `if __name__` clause is a call to a main function, but it can be any code that you want. Some files simply put their main code there even if its dozens of lines. + +If there is just a single function call in the `if __name__` clause, it's often called `main()`, but that name isn't special to Python. You can call any code you like. Many people use `main()` because it's a clear indication of what's going on. + + +## Why would I do this? + +There are a few reasons for using this structure, mostly about having two different uses for the file, one if it's run directly, and a different one for when it's imported as part of a larger program. + +The most common reason is your module is a library, but also has a special case where it can be run directly. Sometimes libraries have small utility or demonstration scripts provided in the `__main__` clause. + +Even if you don't intend your file to be used in two different ways, the convention of `if __name__ == "__main__":` gives a clear indication to the reader that this is the main file of the program. + + +## Why not have a `main` keyword? + +Python often uses so-called "dunder" names for special behavior. The global name `__name__` lets files know how they are being run: directly or imported. Other languages might have a special name for the main function. Python instead lets you write your own if statement using `__name__` to decide what code should run be as the main body. From e8e1e94f863d358263083197253d92fa61f4b6ad Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Fri, 6 Mar 2026 06:24:45 -0500 Subject: [PATCH 2/3] fix quotes --- .../content/resources/guides/python-guides/if-name-main.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pydis_site/apps/content/resources/guides/python-guides/if-name-main.md b/pydis_site/apps/content/resources/guides/python-guides/if-name-main.md index 3509b9f20..30ffec880 100644 --- a/pydis_site/apps/content/resources/guides/python-guides/if-name-main.md +++ b/pydis_site/apps/content/resources/guides/python-guides/if-name-main.md @@ -1,6 +1,6 @@ --- -title: "`if __name__ == "__main__"`" -description: "What is `if __name__ == "__main__"`, and why would you use it?" +title: '`if __name__ == "__main__"`' +description: 'What is `if __name__ == "__main__"`, and why would you use it?' --- You might see this structure in a Python file: From 7d78e69bc70f757c36ce883f0a3fee0cc0604b39 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Fri, 6 Mar 2026 07:33:06 -0500 Subject: [PATCH 3/3] add other resources --- .../content/resources/guides/python-guides/if-name-main.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pydis_site/apps/content/resources/guides/python-guides/if-name-main.md b/pydis_site/apps/content/resources/guides/python-guides/if-name-main.md index 30ffec880..91c405ae3 100644 --- a/pydis_site/apps/content/resources/guides/python-guides/if-name-main.md +++ b/pydis_site/apps/content/resources/guides/python-guides/if-name-main.md @@ -58,3 +58,10 @@ Even if you don't intend your file to be used in two different ways, the convent ## Why not have a `main` keyword? Python often uses so-called "dunder" names for special behavior. The global name `__name__` lets files know how they are being run: directly or imported. Other languages might have a special name for the main function. Python instead lets you write your own if statement using `__name__` to decide what code should run be as the main body. + + +## Other resources + +- The Python docs have a section on [Idiomatic usage of `__main__`](https://docs.python.org/3/library/__main__.html#idiomatic-usage). + +- Real Python has a [longer tutorial on `__main__`](https://realpython.com/if-name-main-python/).