470,613 Members | 2,344 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,613 developers. It's quick & easy.

embedded python in c - function

Hi guys,

I am pretty new to Python and to embedding it in C.
Here is what I am trying to accomplish:

I have a C program that accepts user input, which can be
a Python script which includes functions defined by the user line by
line as typed in. Let me show the code that I have:

#include "Python.h"
main(int argc, char **argv)
// Pass argv[0] to the Python interpreter

// Initialize the Python interpreter. Required.
// Define sys.argv. It is up to the application if you
// want this; you can also let it undefined (since the Python
// code is generally not a main program it has no business
// touching sys.argv...)
PySys_SetArgv(argc, argv);

PyRun_SimpleString("def fib(n):\n");
PyRun_SimpleString("\t\ta, b = 0, 1\n");
PyRun_SimpleString("\t\t\while b < n:n");
PyRun_SimpleString("\t\tprint b,\n");
PyRun_SimpleString("\t\t\a, b = b, a+bn");
// Exit, cleaning up the interpreter


The code gives me the following:

File "<string>", line 1
def fib(n):
SyntaxError: unexpected EOF while parsing
File "<string>", line 1
a, b = 0, 1
SyntaxError: invalid syntax
File "<string>", line 1
while b < n:n
SyntaxError: invalid syntax
File "<string>", line 1
print b,
SyntaxError: invalid syntax
File "<string>", line 1
, b = b, a+bn
SyntaxError: invalid syntax
Traceback (most recent call last):
File "<string>", line 1, in ?
Could anybody help me out by either pointing to a tutorial that deals
with this type of embedding to point out my mistakes in the code!

Jul 19 '05 #1
0 968

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by lebo | last post: by
13 posts views Thread by Alexander May | last post: by
2 posts views Thread by fotis | last post: by
3 posts views Thread by Godzilla | last post: by
3 posts views Thread by Torsten Bronger | last post: by
20 posts views Thread by Jack | last post: by
reply views Thread by PlayDough | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.