ListArc » English » Computer and Internet » NumPy-Discussion » Cython and f2py


Cython and f2py


02-07-2010 07:53 PM
1


Hi All,

Sorry if this has been documented or discussed already, but my searches have come up short. Can someone please recommend a way to setup both Cython and Fortran extensions in a single package with numpy.distutils (or something else)? E.g.:

from numpy.distutils.core import setup, Extension
ext_modules = [
Extension( 'cext', ['cext.pyx'] ),
Extension( 'fext', ['fext.f90'] ),
]
setup(
name = 'test',
ext_modules = ext_modules,
)

Can numpy.distutils be directed to process *.pyx with Cython rather than Pyrex?

Is is kosher to call setup twice in the same script, once for Fortran, and once for Cython using Cython.Distutils.build_ext, or would that do bad things?

I guess I could just pre-process the Cython stuff, and have distutils work with the generated C, but I don't like that as much.

I am using numpy version 1.4.0, but can update to the development version if that helps.

Thanks,
Geoff

_______________________________________________
___________________________________________________

Posted on the NumPy-Discussion mailing list. Go to http://mail.scipy.org/mailman/listinfo/numpy-discussion to subscribe.


02-07-2010 08:00 PM
2


On Fri, Jul 2, 2010 at 12:53 PM, Geoffrey Ely <> wrote:
> Hi All,
>
> Sorry if this has been documented or discussed already, but my searches have come up short. Can someone please recommend a way to setup both Cython and Fortran extensions in a single package with numpy.distutils (or something else)? E.g.:
>
> from numpy.distutils.core import setup, Extension
> ext_modules = [
>    Extension( 'cext', ['cext.pyx'] ),
>    Extension( 'fext', ['fext.f90'] ),
> ]
> setup(
>    name = 'test',
>    ext_modules = ext_modules,
> )
>
> Can numpy.distutils be directed to process *.pyx with Cython rather than Pyrex?
>
> Is is kosher to call setup twice in the same script, once for Fortran, and once for Cython using Cython.Distutils.build_ext, or would that do bad things?
>
> I guess I could just pre-process the Cython stuff, and have distutils work with the generated C, but I don't like that as much.

That's what I do. I think it is easier for users (some might not have
cython). If the C extension doesn't compile I fallback to the python
versions of the functions.

http://github.com/kwgoodman/la/blob/master/setup.py
_______________________________________________
___________________________________________________

Posted on the NumPy-Discussion mailing list. Go to http://mail.scipy.org/mailman/listinfo/numpy-discussion to subscribe.


02-07-2010 08:07 PM
3


Hi,

> Can numpy.distutils be directed to process *.pyx with Cython rather than Pyrex?

Yes, but at the moment I believe you have to monkey-patch numpy
distutils : see the top of

http://github.com/matthew-brett/nipy/blob/master/setup.py

and "generate_a_pyrex_source" around line 289 of:

http://github.com/matthew-brett/nipy/blob/master/build_helpers.py

for how we've done it - there may be a better way - please post if you find it!

Best,

Matthew
_______________________________________________
___________________________________________________

Posted on the NumPy-Discussion mailing list. Go to http://mail.scipy.org/mailman/listinfo/numpy-discussion to subscribe.


05-07-2010 09:31 PM
4


On Jul 2, 2010, at 1:07 PM, Matthew Brett wrote:

>> Can numpy.distutils be directed to process *.pyx with Cython rather than Pyrex?
>
> Yes, but at the moment I believe you have to monkey-patch numpy
> distutils : see the top of
>
> http://github.com/matthew-brett/nipy/blob/master/setup.py
>
> and "generate_a_pyrex_source" around line 289 of:
>
> http://github.com/matthew-brett/nipy/blob/master/build_helpers.py
>
> for how we've done it - there may be a better way - please post if you find it!

Thanks Matthew. I don't know enough about distutils to quite follow everything that you have done. So I think I will get by for now by calling setup separately as in the code below. It seems to be working for installing from source, which is my main goal. Anyone know if this is a bad idea?

-Geoff

# Cython extension
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
setup(
ext_modules = [Extension( 'cext', ['cext.pyx'] )],
cmdclass = {'build_ext': build_ext},
script_args = ['build_ext', '--inplace'],
)

# Fortran extension
from numpy.distutils.core import setup, Extension
setup(
ext_modules = [Extension( 'fext', ['fext.f90'] )],
)

_______________________________________________
___________________________________________________

Posted on the NumPy-Discussion mailing list. Go to http://mail.scipy.org/mailman/listinfo/numpy-discussion to subscribe.