When you run Python interactively the local __name__
variable is assigned a value of __main__
. Likewise, when you execute a Python module from the command line, rather than importing it into another module, its __name__
attribute is assigned a value of __main__
, rather than the actual name of the module. In this way, modules can look at their own __name__
value to determine for themselves how they are being used, whether as support for another program or as the main application executed from the command line. Thus, the following idiom is quite common in Python modules:
if __name__ == '__main__':
# Do something appropriate here, like calling a
# main() function defined elsewhere in this module.
main()
else:
# Do nothing. This module has been imported by another
# module that wants to make use of the functions,
# classes and other useful bits it has defined.