PythonSyntax
Main Guard
Code example
if __name__ == "__main__":Runs code only when the file is run directly.
In Python, main guard: runs code only when the file is run directly — if __name__ == "__main__":
if __name__ == "__main__":if __name__ == "__main__":Runs code only when the file is run directly.
Use if __name__ == "__main__":.
Use this pattern: if __name__ == "__main__":
Yes. Use Copy code and adapt the example to your program.
No. The language syntax is the same across operating systems.