What is the meaning of __name__ == "__main__" in Python?

Why __name__ == "__main__" is used as a condition in python.

When a file executes it sets some special variables and __name__ is one of them. __name__ is set as "__main__" if it is getting executed directly. But if the file has been imported then it stores the filename suppose.

For example:


if __name__ == "__main__":
    print("Executed directly", __name__ )
    print("Import executed", __name__ )

### On execution of abc.py directly __name__ will store "__main__".


import abc
print "Import executed: ", __name__  

### On execution of xyz.py, below output will display:

Import executed abc.py
Import executed __main__