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

匆匆过客 提交于 2020-01-11 22:59:12

问题


I would like to create a setup.py script for a python package with several submodules that depend on both cython and f2py. I have attempted to use setuptools and numpy.distutils, but have so far failed:

Using setuptools

I am able to compile my cython extensions (and create an installation for the rest of the package) using setuptools. I have, however, been unable to figure out how to use setuptools to generate the f2py extension. After extensive searching, I only found rather old messages like this one that state that f2py modules must be compiled using numpy.distutils.

Using numpy.distutils

I am able to compile my f2py extensions (and create an installation for the rest of the package) using numpy.distutils. I have, however, been unable to figure out how to get numpy.distutils to compile my cython extensions as it always attempts to use pyrex to compile it (and I am using extensions specific to cython) recent. I have done a search to figure out how to get numpy.distutils for cython files and - at least as of a year ago - they recommend applying a monkey patch to numpy.distutils. It seems applying such a monkey patch also restricts the options that can be passed to Cython.

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


回答1:


You can just call each separately in your setup.py 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)
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()




回答2:


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 setup
from 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_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 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.



来源:https://stackoverflow.com/questions/7932028/setup-py-for-packages-that-depend-on-both-cython-and-f2py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!