views:

47

answers:

1

I'm trying to embed python into c to use it for configuration:

If I do it like this:

/******************************************************************************
* 
* Embeding Python Example
*
* To run: 
*   gcc -c test.c -o test.o -I"C:/Python25/include"
*   gcc -o test.exe test.o -L"C:/Python25/libs" -lpython25
*   test.exe
*
******************************************************************************/

#include <Python.h>

int main(int argc, char *argv[])
{
    PyObject *run;
    PyObject *globals = PyDict_New();
    PyObject *locals = PyDict_New();

    Py_Initialize();

    run = PyRun_String("from time import time,ctime\n"
                       "print 'Today is',ctime(time())\n"
                        "test = 5\n"
                        "print test\n", Py_file_input, globals, locals);


    Py_Finalize();
    return 0;
}

I'm getting a runtime error from the the Microsoft VIsual C++ Runtime, with this message:

Exception exceptions.ImportError: '__import__ not found' in 'garbage collection' ignored
Fatal Python error: unexpected exception during garbage collection

What am I doing wrong?

+1  A: 

You're creating 2 Python objects before you've initialized the Python engine.

Also, that's silly. There are plenty of JSON parsers for C.

Ignacio Vazquez-Abrams
+1. Why do you want to embed a complete scripting language interpreter into your app *just* to use as a configuration language?
Noufal Ibrahim
I tried initializing the objects after inintializing the Python engine, but the error still there.
zerodx