PyRun_SimpleString problem.

Gordon McMillan gmcm at
Wed Nov 10 12:11:46 EST 1999

"Arinte" writes:

> I am trying to run a python file from a dll.  When I run the file
> w/ PyRun_AnyFile it works fine.  But, when I try to run the code
> line by line w/ PyRun_SimpleString I get this error on function
> definitions.
> def bottle(n):
>   File "<string>", line 1
>     def bottle(n):
>                 ^
> SyntaxError: unexpected EOF while parsing
>     if n == 0: return "no more bottles of beer"
>   File "<string>", line 1
>     if n == 0: return "no more bottles of beer"
> Can I run files line by line if they define functions?  If so,
> how?

Nope. Can't do it with exec either. Even the interactive 
interpreter can't get away with it. You can only exec / eval a 
complete syntactic unit. You could look at a shell (eg, to see how's it done interactively, (normally append 
the line to a buffer, try running the buffer; if it worked, clean out 
the buffer). But if PyRun_AnyFile works, I wonder why you're 
even trying PyRun_SimpleString? Python would much prefer 
to work at the file level.

- Gordon

More information about the Python-list mailing list