469,607 Members | 1,889 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,607 developers. It's quick & easy.

ImportError while Embedding python in C

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
1 3157
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.

Similar topics

reply views Thread by Carsten Gehling | last post: by
reply views Thread by M. Lavasani | last post: by
2 posts views Thread by Tero Pihlajakoski | last post: by
4 posts views Thread by Alicia Haumann | last post: by
2 posts views Thread by Adam Blinkinsop | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.