Understand __name__==’main’ with two python files



This content originally appeared on DEV Community and was authored by Srinivasulu Paranduru

Understand name==’main‘ using two python files

one.py

#one.py
def func():
    print("FUNC() IN one.py")

print("TOP LEVEL IN one.py")
if __name__ == '__main__':
    print('one.py is being run directly')
else:
    print('one.py has been imported')

two.py

#two.py
import one
print("TOP LEVEL IN TWO.PY")
one.func()

if __name__ == '__main__':
    print('TWO.PY is being run directly!')
else:
    print("TWO.PY has been imported!")

  • try executing one by one python files and see the output how better understanding name == ‘main


This content originally appeared on DEV Community and was authored by Srinivasulu Paranduru