[Pyrex] A "main" function for Pyrex: cimport does not work

len-l at telus.net len-l at telus.net
Fri Apr 15 20:30:23 CEST 2005


On 15 Apr 2005 at 6:40, Alain Pointdexter wrote:

>  --- len-l at telus.net a écrit : 
> > This simple example taken from the Pyrex docs ran
> > for me:
> >
[Snip example having dishes.pyx and restaurant.pyx files]
> > 
> > dishes.pxd and restaurant.pyx were in the same
> > directory.
> 
> Thank you for responding so quickly Lenard. I've just
> tried your example. I entered the following commands:
> pyrexc restaurant.pyx
> gcc -c restaurant.c -I/usr/local/Python-2.2.2/Include 
>  -D__pymoduleinit__=initrestaurant
> pyrexc dishes.pyx
> gcc -c dishes.c -I/usr/local/Python-2.2.2/Include
> gcc -o restaurant restaurant.o dishes.o
> -L/usr/local/Python-2.2.2 -lpython2.2 -ldl -lpthread
> -lutil -lm  -D__pymoduleinit__=initrestaurant
> 
I am not an expert on Gcc and Unix so I may be wrong but
it looks like you are staticly linking module dishes into
your program. I built dishes.pyx as a separate dynamic
library ( .so file ?). So when the dishes module is imported
the library is loaded by Python and its initdishes function is
called. When a module is statically linked into a program
its init function must be called explicitly to import the module.

I suggest using distutils to build dishes.pyx as a separate
module then link restaurant.o by itself.

Here is the setup.py file to use on dishes.pyx:

====================================
from distutils.core import setup
from distutils.extension import Extension
from Pyrex.Distutils import build_ext

setup(
  name = 'dishes',
  ext_modules=[ 
    Extension("dishes",       ["dishes.pyx"]),
    ],
  cmdclass = {'build_ext': build_ext}
)
=====================================

Lenard Lindstrom
<len-l at telus.net>




More information about the Pyrex mailing list