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

I would like to create a setup.py script for a python package with several submodules that depend on both cython and f2py. I tried using setuptools and numpy.distutils, but still failed:

Using setuptools

I can compile my cython extensions (and create an installation for the rest of the package) using setuptools. However, I was not able to figure out how to use setuptools to generate the f2py extension. After an extensive search, I found fairly old posts like this one that say f2py modules should be compiled with numpy.distutils.

Using numpy.distutils

I can compile my f2py extensions (and create an installation for the rest of the package) using numpy.distutils. However, I have not been able to figure out how to get numpy.distutils to compile my cython extensions, since it always tries to use pyrex to compile it (and I am using cython specific extensions) lately. I did a search to figure out how to get numpy.distutils for cython files and - at least a year ago - they recommend applying the monkey patch before numpy. Distutils Applying such a monkey patch also seems to limit the possibilities that can be passed to Cython.

My question is: what is the recommended way to write setup.py script for packages that depend on both f2py and cython? Is applying the patch to numpy.distutils really a way to still stay?

+16
python numpy cython f2py
Oct 28 '11 at 16:24
source share
2 answers

You can simply call them separately in the setup.py file, as in
http://answerpot.com/showthread.php?601643-cython%20and%20f2py

# 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'] )], ) 

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

The first call to setup () is distutils.extension.Extension
and distutils.core.setup ()

The second setup () call is numpy.distutils.core.Extension
and numpy.distutils.core.setup ()

+4
Dec 14 '11 at 19:42
source share

It turns out that this is no longer the case. With setuptools and distutils (at least the numpy version) it is possible to have extensions with C, Cython, and f2py. The only caveat is that you must always use numpy.distutils for the setup and Extension functions to compile f2py modules. But setuptools can still be used for 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 setup from numpy.distutils.extension import Extension 

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

 import setuptools 

And then the rest of the code is identical:

 from numpy import get_include from Cython.Build import cythonize NAME = '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 other things in the setup() call. In the above example, I assume that you will use numpy with Cython along with the external C file ( my_ext_c_file.c ), which will be in my_c_dir/ , and that the f2py module is only one Fortran file. Adjust if necessary.

+1
Jan 27 '17 at 14:21
source share



All Articles