importing swig generated modules in C program

Tim Fuehner tim.fuehner at t-online.de
Mon Jun 23 05:47:37 EDT 2003


Hi,

I'm pretty new to Python, and I have the following problem:

I extended Python (version 2.2.1 GCC 3.2) by wrapping a C module using SWIG:

C file:
/* File : example.c */

double  My_variable  = 3.0;

/* Compute factorial of n */
int  fact(int n) {
	if (n <= 1) return 1;
	else return n*fact(n-1);
}

/* Compute n mod m */
int my_mod(int n, int m) {
	return(n % m);
}


SWIG file:
/* File : example.i */
%module example
%{
/* Put headers and other declarations here */
%}

extern double My_variable;
extern int    fact(int);
extern int    my_mod(int n, int m);

running ``swig -python example.i'' (version 1.3.19) and using 
``gcc -shared example.o example_wrap.o -o _example.so \
    -L/usr/lib/python2.2/config -lpython2.2''
I obtained the files ``_example.so'' and ``example.py''. Using these
files interactively works fine:
#>>> import example
#>>> example.fact(5)
#120

However, when I try to call this module from within a C program it
fails:

#include <Python.h>

static PyObject *my_callback = NULL;

int main(int argc, char **argv)
{
	Py_Initialize();

	PyRun_SimpleString("import example");

	return 0;
}

yielding:
Fatal Python error: Interpreter not initialized (version mismatch?)
Aborted

the program was linked as follows:
gcc -o pyc pyc.o -lm -lpthread -ldl -lutil \
    -L/usr/lib/python2.2/config -lpython2.2

When I import native Python modules everything works fine.

Anybody have a clue?

-- 
TIM FUEHNER                              mailto: tim.fuehner at t-online.de




More information about the Python-list mailing list