Python Language The __name__ special variable __name__ == '__main__'

30% OFF - 9th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY9


The special variable __name__ is not set by the user. It is mostly used to check whether or not the module is being run by itself or run because an import was performed. To avoid your module to run certain parts of its code when it gets imported, check if __name__ == '__main__'.

Let be just one line long:


And let's see what happens, depending on

Situation 1


Running will print hello
Running will print hello

Situation 2

if __name__ == '__main__':

Running will print nothing
Running will print hello

Got any Python Language Question?