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'
Top comments (0)