setup.py for packages that depend on both cython and f2py setup.py for packages that depend on both cython and f2py python python

setup.py for packages that depend on both cython and f2py


You can just call each separately in your setup.py as in
http://answerpot.com/showthread.php?601643-cython%20and%20f2py

# Cython extensionfrom distutils.core import setupfrom distutils.extension import Extensionfrom Cython.Distutils import build_extsetup(  ext_modules = [Extension( 'cext', ['cext.pyx'] )],  cmdclass = {'build_ext': build_ext},  script_args = ['build_ext', '--inplace'],)# Fortran extensionfrom numpy.distutils.core import setup, Extensionsetup(  ext_modules = [Extension( 'fext', ['fext.f90'] )],)

Your calling context (I think they call this namespace, not sure)
has to change as to what the current object Extension and function
setup() is.

The first setup() call, it's the distutils.extension.Extension
and distutils.core.setup()

The second setup() call, it's the numpy.distutils.core.Extension
and numpy.distutils.core.setup()


Turns out this is no longer true. With both setuptools and distutils (at least the numpy version) it is possible to have extensions with C, Cython and f2py. The only caveat is that to compile f2py modules one must always use numpy.distutils for both the setup and Extension functions. But setuptools can still be used for the installation (for example, allowing the installation of a developer version with python setup.py develop).

To use distutils exclusively you use the following:

from numpy.distutils.core import setupfrom numpy.distutils.extension import Extension

To use setuptools, you need to import it before the distutils imports:

import setuptools

And then the rest of the code is identical:

from numpy import get_includefrom Cython.Build import cythonizeNAME = 'my_package'NUMPY_INC = get_include()extensions = [    Extension(name=NAME + ".my_cython_ext",               include_dirs=[NUMPY_INC, "my_c_dir"]              sources=["my_cython_ext.pyx", "my_c_dir/my_ext_c_file.c"]),    Extension(name=NAME + ".my_f2py_ext",               sources=["my_f2py_ext.f"]),]extensions = cythonize(extensions)setup(..., ext_modules=extensions)

Obviously you need to put all your other stuff in the setup() call. In the above I assume that you'll use numpy with Cython, along with an external C file (my_ext_c_file.c) that will be at my_c_dir/, and that the f2py module is only one Fortran file. Adjust as needed.