By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,550 Members | 1,161 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,550 IT Pros & Developers. It's quick & easy.

ImportError while Embedding python in C

P: n/a
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
Mar 15 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Mar 15, 2:10 am, kaush <kaushikba...@gmail.comwrote:
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
Mar 15 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.