What is if __name__ == '__main__' ?

Every module in Python has a special attribute called __name__. This attribute is set to __main__ when the module is running as the main program.

If you want to share your module or would like to import it into another program. You can use the attribute to ensure certain functions in the module do not run. this is achieved by adding the following conditional to the main program

if __name__ == '__main__':
        do something
    
Tags: python