if __name__ == “__main__” in Python. Topic – 31

if __name__ == __main__ In Python

The if __name__ == “__main__” in Python is essential for writing reusable and modular code, especially when working with modules and scripts. If the modules are third-party modules or maybe your friend gives you in this case if __name__ == “__main__” is more useful. Let’s break it down step by step, from beginner to advanced level.

Understanding __name__ Concepts:

Python assigns a special variable __name__ to every module. When you run a Python script directly, __name__ is set to “__main__“. When you import a module into another script, __name__ becomes the module’s name (e.g., “cybercript”).

The if __name__ == “__main__“: Construct:

This is basically a condition that check your programme is runs directly or run after importing to another file.

It’s a conditional statement that checks the value of __name__. The code within the if block executes only when the script is run directly (i.e., __name__ is “__main__“). When imported, the code outside the if block is executed, making functions and classes from the module available to us.

Let’s implement this into the code:

Create a Module (my_module.py):

def greet(name):
    print (f" Hello, {name}")


If We just import this code into the another file and run, this file will be autometically exicute. That may be risky for us. So avoid this risk we can use if __name__ == “__main__” method.

def greet(name):
    print (f" Hello, {name}")

if __name__ == "main":

Now the code will not execute automatically. And this is completely safe for us. Now if we want to execute this program we can do this using our standard method.

import my_module

# Output: Hello, Yahika!
my_module.greet("Yashika")  # This will work because greet() is defined in my_module.py

We can also check the value of __name__ using print statement. So, let’s see what is the __name__ actually

def greet(name):
    print (f" Hello, {name}")
    # output main / output: my_module

if __name__ == "main":


If we run this code from the same file then it will return main. If we import this into another file and then check the __name__ it will display the file name from where you will import it.

It’s basically a checkpoint. Where we can check if the file is __name__ is main then execute, otherwise don’t.

Why Use It?

  • Preventing Unintended Execution:

    Using if __name__ == “__main__“: allows you to prevent certain code (e.g., setup code, initialization code) from executing unintentionally when a module is imported.

    Let me share a personal experience. My friend created a module and gave me the code to check on my laptop. Without fully understanding the code, I imported it and ran just one function. However, to my surprise, my files were deleted. When I asked my friend about the code, he explained that it was designed to permanently delete files. Confused, I wondered how running just one function could trigger the deletion of all my files. It turns out, he hadn’t used the if __name__ == “__main__” statement, so upon importing the module, all the code within it, including the deletion function, was executed.

  • Run insider code:

    When you import a module in Python, the code in that module is executed. However, sometimes you only want certain parts of the module to execute when it’s run directly, not when it’s imported.

  • Modular Code:

    You can create reusable code in modules without unintended execution when imported.

  • Testing and Debugging:

    Place test code or debugging statements within the if block to run them only when the script is executed directly.

  • Entry Point:

    Define a main() function within the if block to be the starting point when running the script directly.

I hope you understand “if __name__ == “__main__” in Python” Need guidance or have questions? Drop a comment below. Share your email for a personalized touch—I’ll send exclusive resources and answer queries. Let’s code together, creating efficient code and memorable learning moments! 🚀

Finite number of  lines code and infinite possibilities

Programmer, Arpan Bera

Leave a Reply

Your email address will not be published. Required fields are marked *

Arpan Bera

Coding is like Love – everyone can start, but only a few finish without errors

Keep Updated to our News and Blog