NameError: name '__main__' is not defined

Discussion in 'Python' started by Peng Yu, Sep 14, 2009.

  1. Peng Yu

    Peng Yu Guest

    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
    Peng Yu, Sep 14, 2009
    #1
    1. Advertising

  2. Peng Yu

    André Guest

    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é
    André, Sep 14, 2009
    #2
    1. Advertising

  3. Peng Yu

    Sean DiZazzo Guest

    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
    Sean DiZazzo, Sep 14, 2009
    #3
  4. Peng Yu

    Sean DiZazzo Guest

    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
    Sean DiZazzo, Sep 14, 2009
    #4
  5. Peng Yu

    Andre Engels Guest

    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,
    Andre Engels, Sep 14, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jolly
    Replies:
    2
    Views:
    565
    jolly
    Dec 19, 2007
  2. Replies:
    3
    Views:
    584
    Dan Bishop
    Mar 22, 2008
  3. Lalit
    Replies:
    1
    Views:
    729
    Diez B. Roggisch
    Apr 24, 2008
  4. Mathieu Prevot
    Replies:
    3
    Views:
    360
    Mathieu Prevot
    May 24, 2008
  5. Hendrik van Rooyen

    Re: NameError: name '__main__' is not defined

    Hendrik van Rooyen, Sep 14, 2009, in forum: Python
    Replies:
    0
    Views:
    430
    Hendrik van Rooyen
    Sep 14, 2009
Loading...

Share This Page