NameError: name '__main__' is not defined

Hendrik van Rooyen hendrik at microcorp.co.za
Mon Sep 14 10:03:38 CEST 2009


On Monday 14 September 2009 03:43:19 Peng Yu wrote:
> Hi,
>
> I try the following code. I don't quite understand why __main__ is not
> defined. Could somebody let me know what I am wrong about it?
>
> Regards,
> Peng
>
> $ cat test.py
> #!/usr/bin/env python
>
> if __main__ == '__main__' :
>   print "Hello World!\n"
> $ ./test.py
> Traceback (most recent call last):
>   File "./test.py", line 3, in <module>
>     if __main__ == '__main__' :
> NameError: name '__main__' is not defined

You are looking for __name__ , not __main__

if __name__ == '__main__':
    """We are at the top of the tree so run something"""
    print "Goodbye Cruel World"

- Hendrik




More information about the Python-list mailing list