Python - if __name__ == '__main__'
** if __name__ == '__main__'
==========================================
print(__name__)
---------------------------------
__main__
---------------------------------
==========================================
print("First Module's Name: {}".format(__name__))
---------------------------------
First Module's Name: __main__
---------------------------------
==========================================
import first_module
-------first_module.py---------------
print("First Module's Name: {}".format(__name__))
---------------------------------
First Module's Name: first_module
---------------------------------
==========================================
import first_module
print("Second Module's Name: {}".format(__name__))
-------first_module.py---------------
print("First Module's Name: {}".format(__name__))
---------------------------------
First Module's Name: first_module
Second Module's Name: __main__
---------------------------------
==========================================
def main():
print("First Module's Name: {}".format(__name__))
if __name__ == '__main__':
main()
---------------------------------
First Module's Name: __main__
---------------------------------
==========================================
import first_module
print("Second Module's Name: {}".format(__name__))
-------first_module.py---------------
def main():
print("First Module's Name: {}".format(__name__))
if __name__ == '__main__':
main()
---------------------------------
Second Module's Name: __main__
---------------------------------
==========================================
import first_module
print("Second Module's Name: {}".format(__name__))
-------first_module.py---------------
if __name__ == '__main__':
print("Run Directly")
else:
print("Run From Import")
---------------------------------
Run From Import
Second Module's Name: __main__
---------------------------------
==========================================
print("This will always be run")
def main():
print("First Module's Main Method")
if __name__ == '__main__':
main()
---------------------------------
This will always be run
First Module's Main Method
---------------------------------
==========================================
import first_module
print("Second Module's Name: {}".format(__name__))
-------first_module.py---------------
print("This will always be run")
def main():
print("First Module's Main Method")
if __name__ == '__main__':
main()
---------------------------------
This will always be run
Second Module's Name: __main__
---------------------------------
==========================================
import first_module
first_module.main()
print("Second Module's Name: {}".format(__name__))
-------first_module.py---------------
print("This will always be run")
def main():
print("First Module's Main Method")
if __name__ == '__main__':
main()
---------------------------------
This will always be run
First Module's Main Method
Second Module's Name: __main__
---------------------------------