if __name__=='__main__': main() 
Author Message
 if __name__=='__main__': main()

I am trying to run a script from IDLE and
I get an error message saying

    main()
         ^
SyntaxError: invalid syntax

main() is properly defined, the same script
runs under PythonWin.

How do you go about running a script from
inside IDLE?

Thank you in advance,
Jaime



Tue, 07 Jun 2005 23:01:10 GMT  
 if __name__=='__main__': main()
jaime> I am trying to run a script from IDLE and
jaime> I get an error message saying

jaime>     main()
jaime>          ^
jaime> SyntaxError: invalid syntax

I'll bet it's an indentation error or something like that.

Can you post the entire code, or at least enough context
to make it clear what's going on?

--



Wed, 08 Jun 2005 00:21:25 GMT  
 if __name__=='__main__': main()

Quote:

> I am trying to run a script from IDLE and
> I get an error message saying

>     main()
>          ^
> SyntaxError: invalid syntax

> main() is properly defined, the same script
> runs under PythonWin.

> How do you go about running a script from
> inside IDLE?

> Thank you in advance,
> Jaime

Does your code look something like this?

if __name__ == '__main__':
    main()

def main():
    print 'spam'

If so, then you will need to move the definition before its use, like so:

def main():
    print 'spam'

if __name__ == '__main__':
    main()

Andy



Wed, 08 Jun 2005 10:15:53 GMT  
 if __name__=='__main__': main()

Quote:


> > I am trying to run a script from IDLE and
> > I get an error message saying

> >     main()
> >          ^
> > SyntaxError: invalid syntax

> Does your code look something like this?

> if __name__ == '__main__':
>     main()

> def main():
>     print 'spam'

> If so, then you will need to move the definition before its use, like so:

This would lead to a NameError, not a SyntaxError, so it's unfortunately
not likely the cause, valid though your advice is.

-Peter



Wed, 08 Jun 2005 15:33:48 GMT  
 if __name__=='__main__': main()

Quote:



>> > I am trying to run a script from IDLE and
>> > I get an error message saying

>> >     main()
>> >          ^
>> > SyntaxError: invalid syntax

>> Does your code look something like this?

>> if __name__ == '__main__':
>>     main()

>> def main():
>>     print 'spam'

>> If so, then you will need to move the definition before its use, like so:

> This would lead to a NameError, not a SyntaxError, so it's unfortunately
> not likely the cause, valid though your advice is.

Very nicely put, thank you.  It's amazing how easy it is to to overlook
facts when you're "sure" of the answer.

Thanks again,

Andy



Thu, 09 Jun 2005 11:51:40 GMT  
 if __name__=='__main__': main()

Quote:

> jaime> I am trying to run a script from IDLE and
> jaime> I get an error message saying

> jaime>     main()
> jaime>          ^
> jaime> SyntaxError: invalid syntax

> I'll bet it's an indentation error or something like that.

> Can you post the entire code, or at least enough context
> to make it clear what's going on?

Andrew,
Thank you for your reply. Even though I know the
source of the problem now, I am attaching the code
so that maybe somone can learn from my mistake.

def hello(msg):
    print "Hello, " + msg

def main():
    import sys
    print "Script name is", sys.argv[0]
    if len(sys.argv)>=2:
        hello(sys.argv[1])
    else:
        hello("Please say something next time")

if __name__=='__main__':
    main()

THE PROBLEM is that I had to enter a newline after
main()!!

Thank you very much for your help



Sat, 11 Jun 2005 00:20:51 GMT  
 if __name__=='__main__': main()

Quote:

> if __name__=='__main__':
>     main()

> THE PROBLEM is that I had to enter a newline after
> main()!!

Here's a late response to this issue.  I was just butting heads with
it on Tuesday, so the it seems fresh for me.

This error only occurs if you are parsing a string.  Python's parser
has separate APIs for parsing from a file (a C FILE *) and from a
string (a C char *).  The file version of the parser APIs doesn't
require a trailing newline, but the string version does.  This
difference is a bug in the implementation.  Neither version of the API
should care about the trailing newline.

Jeremy



Tue, 21 Jun 2005 22:30:38 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Help with __name__ == '__main__'

2. Newbie trying to understand __name__=='__main__'

3. tracing a program using __import__('__main__')?

4. Accessing one's main script's global from imported libraries

5. What does MAIN_ _MAIN_ __MAIN__ mean?(to invoke clapack)

6. ENTRY in main (was re: Jumping from nested subroutine to main directly)

7. python __main__ is similar to java main()

8. Hiding main TK main window

9. ATTACHX problem: task doesn't terminate after termination of main program

10. C5b Can't Compile: Missing Procedure: MAIN

11. Changing the MAin Window's Caption

12. SubVI's in Main VI

 

 
Powered by phpBB® Forum Software