NameError: name '__main__' is not defined

P

Peng Yu

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
 
A

André

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é
 
S

Sean DiZazzo

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
 
S

Sean DiZazzo

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
 
A

Andre Engels

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top