Why can't I import the "math" library when embedding python in c?

I'm using the example in the python 2.6 docs to start a foray into nesting some python in C. C-code example don't let me execute the following 1 line script:

import math

      

Using the string:

./tmp.exe tmp foo bar

      

he complains

Traceback (most recent call last):
  File "/home/rbroger1/scripts/tmp.py", line 1, in <module>
    import math
ImportError: [...]/python/2.6.2/lib/python2.6/lib-dynload/math.so: undefined symbol: PyInt_FromLong

      

When I do nm

in my generated binary (tmp.exe) it shows

0000000000420d30 T PyInt_FromLong

      

The function appears to be defined, so why can't the generic object find the function?

+2


source to share


1 answer


I am using Python 2.6 and I have successfully compiled and run the same example code you provided without changing anything in the source.

$ gcc python.c -I / usr / include / python2.6 / /usr/lib/libpython2.6.so
$ ./a.out random randint 1 100
Result of call: 39
$ ./a.out random randint 1 100
Result of call: 57


I specifically chose the module random

because it has from math import log,...

, so it will make sure to import the module math

.

Your problem is probably related to how you are communicating; see this forum post for a similar problem someone else had. I can't find links again, but it seems that there are some common problems when trying to link with a Python static library and then import modules that require a dynamic library.

+2


source







All Articles