Author |
Message |
jaime suar #1 / 7
|
 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 |
|
 |
Andrew Koeni #2 / 7
|
 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 |
|
 |
Andrew Hensha #3 / 7
|
 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 |
|
 |
Peter Hanse #4 / 7
|
 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 |
|
 |
Andrew Hensha #5 / 7
|
 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 |
|
 |
jaime suar #6 / 7
|
 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 |
|
 |
Jeremy Hylt #7 / 7
|
 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 |
|
|