Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

if __name__=='__main__': main()

1 view
Skip to first unread message

jaime suarez

unread,
Dec 20, 2002, 10:01:10 AM12/20/02
to
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

Andrew Koenig

unread,
Dec 20, 2002, 11:21:25 AM12/20/02
to
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 Koenig, a...@research.att.com, http://www.research.att.com/info/ark

Andrew Henshaw

unread,
Dec 20, 2002, 9:15:53 PM12/20/02
to
jaime suarez wrote:

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

Peter Hansen

unread,
Dec 21, 2002, 2:33:48 AM12/21/02
to
Andrew Henshaw wrote:
>
> jaime suarez wrote:
>
> > 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

Andrew Henshaw

unread,
Dec 21, 2002, 10:51:40 PM12/21/02
to
Peter Hansen wrote:

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

jaime suarez

unread,
Dec 23, 2002, 11:20:51 AM12/23/02
to
Andrew Koenig <a...@research.att.com> wrote in message news:<yu998yyk...@europa.research.att.com>...

> 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

Jeremy Hylton

unread,
Jan 3, 2003, 9:30:38 AM1/3/03
to
jaime....@crossmatch.net (jaime suarez) wrote in message news:<d3f86b28.02122...@posting.google.com>...


> 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

0 new messages