FAQ
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

Search Discussions

  • Xavier Ho at Sep 14, 2009 at 2:25 am
    Try

    if __name__ == '__main__'

    :],
    Xav

    On Mon, Sep 14, 2009 at 11:43 AM, 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
    --
    http://mail.python.org/mailman/listinfo/python-list
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20090914/1d61737f/attachment.htm>
  • André at Sep 14, 2009 at 2:26 am

    On Sep 13, 10:43?pm, 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 wrote __main__ instead of __name__. It should have been:

    if __name__ == '__main__':
    ...

    Andr?
  • Sean DiZazzo at Sep 14, 2009 at 2:29 am

    On Sep 13, 6:43?pm, 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
    Is this a production program that you are using??

    Please show us the point you are trying to make in something more
    valuable.

    ~Sean
  • Andre Engels at Sep 14, 2009 at 7:01 am

    On Mon, Sep 14, 2009 at 4:29 AM, Sean DiZazzo wrote:

    Is this a production program that you are using??

    Please show us the point you are trying to make in something more
    valuable.
    I find this a very bad comment. Not only is it rude, it is condemning
    a behaviour I would see as beneficial. I very much prefer someone
    creating a toy program that is as short and simple as possible while
    still showing their problem than being forced through hundreds of
    lines of code which contain very useful code which however has nothing
    to do with the problem at hand.


    --
    Andr? Engels, andreengels at gmail.com
  • Sean DiZazzo at Sep 14, 2009 at 2:32 am

    On Sep 13, 6:43?pm, 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
    er....I was just angry. __main__ doesn't exist because it is not in
    your programs scope.

    you are looking for:

    if __name__ == "__main__":
    print "Hello World"

    ~Sean
  • Hendrik van Rooyen at Sep 14, 2009 at 8:03 am

    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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedSep 14, '09 at 1:43a
activeSep 14, '09 at 8:03a
posts7
users6
websitepython.org

People

Translate

site design / logo © 2022 Grokbase