ImportError while Embedding python in C

K

kaush

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
 
K

kaush

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top