Day #45 - if __name__ == "__main__" in Python

Day #45 - if __name__ == "__main__" in Python

Introduction

Welcome to my 45th blog post on the Python journey. On day 45, I learned about a very important idiom called as ' ifname == "main" ' in python. This heps us to determine whether the Python scripts is being run directly or being imported as a module into another script. Let's dive into more details and understand the working of ' ifname == "main" '.

So let's get started......

' ifname == "main" '

  • This idiom is a common pattern used to determine whether the Python script that we are running is being run directly or is imported from another script as a module.

  • The __name__ variable is a built-in variable that is automatically set to the name of the current module.

  • When a Python script is run directly, the name variable is set to the string main 

  • When the script is imported as a module into another script, the  __name__   variable is set to the name of the module.

Example -

In the below example, the main function contains the code that should be run when the script is run directly. The if statement at the bottom checks whether the

__name __ variable is equal to ' __ main __ ' . If it is, then the main() function is called.

def main():
    # Code to be run when the script is run directly
    print("Running script directly")

if __name__ == "__main__":
    main()

Importance of ifname == "main"

This idiom allows us to reuse code from a script by importing it as a module into another script, without running the code in the original script. For example, consider the 2 scripts one is main.py and another is welcome.py:

Code in Harry.py

def main():
    print("Running script directly")

if __name__ == "__main__":
    main()

Code in main.py

import harry

harry.welcome()

Outputs when both the files are run

If you run this script directly from harry.py, it will output the message after calling the function. However, if you import it as a module into another script (here main.py) and call the main function from the imported module, it will not output anything directly.

This can be useful if you have code that you want to reuse in multiple scripts, but you only want it to run when the script is run directly and not when it's imported as a module.

Is it a necessity?

It is not compulsory to use the idiom ' ifname == "main" ' to run the Python script. But the idiom can be a useful tool for organizing and separating the code that should be run directly from code that should be imported and used as a module.

In short, the idiom allows you to reuse code from a script by importing it as a module into another script, without running the code in the original script.

Resources Used

You can watch the video of Day#45 by clicking on the below link 👇👇👇👇👇

Conclusion

Thanks, guys for going through this blog post. On day 45, we learned about the idiom ' ifname == "main" ' and its importance n Python script. We also learned about how it allows us to reuse code from a script by importing it as a module into another script, without running the code in the original script.

Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of Python coding. This is it for Day #45

See you in the next one.....


About Me

Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.

I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.

So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.