[SciPy-user] Python embedded in C : import scipy fails

Elisabeth Maschke-Dutz maschke@molgen.mpg...
Fri Mar 9 08:54:02 CST 2007


Hallo,

I want to use some programs that uses scipy
in an in C embedded python program.
import sys
import os
from string import join
..........
all those comands work fine.
The sys.path is found, it looks as if the whole environment
is the same as it is when I start python in a shell.
The directory .../python2.4/site-packages/scipy is also set
in sys.path.

But it is impossible to import scipy,
the program terminates with a segmentation fault or the function is not 
found.
The same problem occurs if I try to import numpy.
It seems that packages, which do not belong to
python can not be importet. Single programs anywhere
do not make any problems if the path is set in sys.path.


The import of scipy in all other python programs do
not make any problems:

 >>> import scipy
 >>>


the testmodel.c code:


#include <Python.h>



int main(int argc, char **argv)
{
  char *modelp = "modelp";
  char *testmodel = "testmodel";
  PyObject *pName, *pModule, *pFunc;
  PyObject *pArgs1, *pArgs2, *pValue, *hallop;
  int i;
  double l;
  l=100.0;
  Py_SetProgramName(argv[0]);
  Py_Initialize();
  PySys_SetArgv(argc, argv);
  pName = PyString_FromString(modelp);
  if (pName != NULL)
    {
      printf("pName != NULL");
    }
  else
    {
      printf("pName == NULL");
    }
  pModule = PyImport_Import(pName);
  Py_DECREF(pName);
  if (Py_IsInitialized())
    printf("Initialized!!!");
  if (pModule != NULL)
    {
     pFunc = PyObject_GetAttrString(pModule,testmodel);
     printf("Function found!!!!!!!!!");
    }
  else
    {
      printf("pModule == NULL!!!!!!!!!");
    }
  if (PyCallable_Check(pFunc))
    printf("function callable");
  pArgs1 = PyFloat_FromDouble(l);
  pArgs2 = PyTuple_New(1);
  i=0;
  PyTuple_SetItem(pArgs2, i,pArgs1);
  if (pArgs2 != NULL)
    printf("pArgs2!=Null");
  pValue = PyObject_CallObject(pFunc, pArgs2);
  Py_DECREF(pArgs1);
  Py_DECREF(pArgs2);
  Py_DECREF(pFunc);
  Py_DECREF(pModule);
  printf("3Result of call...............");
  printf("Result of call: %ld\n", PyInt_AsLong(pValue));
  return 1;
}

the python code:

import sys
import os
from string import join
#from math import *
from string import *
sys.path.append(".................../dir2")
sys.path.append(".......................python2.4/site-packages/scipy")

#import scipy

import testimport  # a single file (program)

def testmodel(a):
    print 'in testmodel................ a= ',a
    print sys.path
    print os.environ
    import distutils.sysconfig
    t1=distutils.sysconfig.get_config_var('LINKFORSHARED')
    print t1
    t2=distutils.sysconfig.get_config_vars()
    print t2
    t3=distutils.sysconfig.get_python_lib()
    print t3
    print 'nach import'
    #import numpy
    return 17


I use gcc with  CFLAGS  = -I/............../linux/include/python2.4 -c

and link  with:


 -pthread -lm -ldl -lutil   
................lib/python2.4/config/libpython2.4.a

.../getpath.o  ..../config.o

Has somebody an idee what I can do?

                  Elisabeth M.



More information about the SciPy-user mailing list