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
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
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
This would lead to a NameError, not a SyntaxError, so it's unfortunately
not likely the cause, valid though your advice is.
-Peter
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
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
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