ImportError while Embedding python in C

kaush kaushikbarat at gmail.com
Sat Mar 15 05:36:19 EDT 2008


On Mar 15, 2:10 am, kaush <kaushikba... at gmail.com> wrote:
> Hi All,
>
> I have a simple python script saved to "test.py" as
>
> import os
> import base64
>
> def Testfunction():
>     print "Hello World"
>     return
>
> Testfunction()
>
> I am trying to invoke this from a C program as follows
>
> int main(int argc, char* argv[])
> {
>         Py_Initialize();
>
>         PyObject* main_module = PyImport_AddModule("__main__");
>
>         PyObject* main_dict = PyModule_GetDict(main_module);
>
>         FILE* file_1 = fopen(TestFile, "r");
>         PyRun_AnyFile(file_1, TestFile);
>
>         Py_Finalize();
>
>         return 0;
>
> }
>
> This fails with the error
>
> Traceback (most recent call last):
>   File "/home/kaushik/shadowFs/test.py", line 4, in <module>
>     import base64
>   File "/usr/local/lib/python2.5/base64.py", line 9, in <module>
>     import struct
>   File "/usr/local/lib/python2.5/struct.py", line 30, in <module>
>     from _struct import Struct, error
> ImportError: /usr/local/lib/python2.5/lib-dynload/_struct.so:
> undefined symbol: PyExc_TypeError
>
> I am able to run test.py successfully from the shell.
>
> What am i missing in importing the base64 library?
>
> Thanks,
> Kaushik

Adding the following link options during compilation solved the
problem "-Xlinker -export-dynamic"

Thanks,
Kaushik



More information about the Python-list mailing list