__name__

test.py



print(__name__)

output



__main__

test2.py



import test

output


test

When test.py is executed directly, str type main is assigned to name. Also, when test.py is imported from the outside, the str type file name test is assigned. In other words

if __name__ == '__main__':
    main()

Main () is not executed when it is imported from the outside, it is executed only when it is executed directly.

Recommended Posts

__name__
Briefly about __name__
Contents of __name__
File name batch change
File name batch conversion
Name identification using python