embedded python in c - function

Discussion in 'Python' started by PixelDust1, May 22, 2005.

  1. PixelDust1

    PixelDust1 Guest

    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
    Py_SetProgramName(argv[0]);

    // Initialize the Python interpreter. Required.
    Py_Initialize();


    // 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");
    PyRun_SimpleString("fib(2000)\n");


    // Exit, cleaning up the interpreter
    Py_Exit(0);

    }

    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!

    Thanks!
    PixelDust1, May 22, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Colin Paul Gloster
    Replies:
    48
    Views:
    1,833
    Colin Paul Gloster
    Apr 10, 2007
  2. Replies:
    5
    Views:
    286
  3. Thomas Dodds

    Databind an embedded control in an embedded datagrid

    Thomas Dodds, Jul 26, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    381
    Thomas Dodds
    Jul 26, 2004
  4. Trans
    Replies:
    11
    Views:
    277
    micathom
    Sep 5, 2007
  5. Num GG
    Replies:
    2
    Views:
    338
    Num GG
    Nov 17, 2008
Loading...

Share This Page