Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > NameError: name '__main__' is not defined

Reply
Thread Tools

NameError: name '__main__' is not defined

 
 
Peng Yu
Guest
Posts: n/a
 
      09-14-2009
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
 
Reply With Quote
 
 
 
 
André
Guest
Posts: n/a
 
      09-14-2009
On Sep 13, 10:43*pm, Peng Yu <(E-Mail Removed)> 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é
 
Reply With Quote
 
 
 
 
Sean DiZazzo
Guest
Posts: n/a
 
      09-14-2009
On Sep 13, 6:43*pm, Peng Yu <(E-Mail Removed)> 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
 
Reply With Quote
 
Sean DiZazzo
Guest
Posts: n/a
 
      09-14-2009
On Sep 13, 6:43*pm, Peng Yu <(E-Mail Removed)> 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
 
Reply With Quote
 
Andre Engels
Guest
Posts: n/a
 
      09-14-2009
On Mon, Sep 14, 2009 at 4:29 AM, Sean DiZazzo <(E-Mail Removed)> 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, http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: __STDC_IEC_559__ (defined or !defined ?) Keith Thompson C Programming 0 08-17-2010 04:36 PM
User-defined exception: "global name 'TestRunError' is not defined" jmike@alum.mit.edu Python 1 07-10-2008 12:37 PM
defined? for recursive function call v/s defined? for function call stack Alok Ruby 3 04-13-2006 11:53 AM
Using parenthesis with defined (#if defined(...)) Angel Tsankov C++ 1 04-05-2006 10:00 PM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM



Advertisments