Recommended way to add Cython extension using numpy.distutils?
Hi, We (neuroimaging.scipy.org) are using numpy.distutils, and we have .pyx files that we build with Cython. I wanted to add these in our current setup.py scripts, with something like: def configuration(parent_package='',top_path=None): from numpy.distutils.misc_util import Configuration config = Configuration('statistics', parent_package, top_path) config.add_extension('intvol', ['intvol.pyx'], include_dirs = [np.get_include()]) return config but of course numpy only knows about Pyrex, and returns: error: Pyrex required for compiling 'nipy/algorithms/statistics/intvol.pyx' but notavailable Is there a recommended way to plumb Cython into the numpy build machinery? Should I try and patch numpy distutils to use Cython if present? Best, Matthew
On Tue, Apr 27, 2010 at 6:09 PM, Matthew Brett
Hi,
We (neuroimaging.scipy.org) are using numpy.distutils, and we have .pyx files that we build with Cython.
I wanted to add these in our current setup.py scripts, with something like:
def configuration(parent_package='',top_path=None): from numpy.distutils.misc_util import Configuration config = Configuration('statistics', parent_package, top_path) config.add_extension('intvol', ['intvol.pyx'], include_dirs = [np.get_include()]) return config
but of course numpy only knows about Pyrex, and returns:
error: Pyrex required for compiling 'nipy/algorithms/statistics/intvol.pyx' but notavailable
Is there a recommended way to plumb Cython into the numpy build machinery? Should I try and patch numpy distutils to use Cython if present?
Patching distutils might be the way to go. We use Cython for the random build now because Pyrex couldn't handle long strings in a way suitable for Windows. Chuck
On Tue, Apr 27, 2010 at 8:09 PM, Matthew Brett
Hi,
We (neuroimaging.scipy.org) are using numpy.distutils, and we have .pyx files that we build with Cython.
I wanted to add these in our current setup.py scripts, with something like:
def configuration(parent_package='',top_path=None): from numpy.distutils.misc_util import Configuration config = Configuration('statistics', parent_package, top_path) config.add_extension('intvol', ['intvol.pyx'], include_dirs = [np.get_include()]) return config
but of course numpy only knows about Pyrex, and returns:
error: Pyrex required for compiling 'nipy/algorithms/statistics/intvol.pyx' but notavailable
Is there a recommended way to plumb Cython into the numpy build machinery? Should I try and patch numpy distutils to use Cython if present?
Here is the monkey-patch I'm using in my project: def evil_numpy_monkey_patch(): from numpy.distutils.command import build_src import Cython import Cython.Compiler.Main build_src.Pyrex = Cython build_src.have_pyrex = True
Hi, Thanks a lot for the suggestion - I appreciate it.
Is there a recommended way to plumb Cython into the numpy build machinery? Should I try and patch numpy distutils to use Cython if present?
Here is the monkey-patch I'm using in my project: def evil_numpy_monkey_patch(): from numpy.distutils.command import build_src import Cython import Cython.Compiler.Main build_src.Pyrex = Cython build_src.have_pyrex = True
I think this patch does not work for current numpy trunk; I've put a minimal test case here: http://github.com/matthew-brett/du-cy-numpy If you run the setup.py there (python setup.py build) then all works fine for - say - numpy 1.1. For current trunk you get an error ending in: File "/Users/mb312/usr/local/lib/python2.6/site-packages/numpy/distutils/command/build_src.py", line 466, in generate_a_pyrex_source if self.inplace or not have_pyrex(): TypeError: 'bool' object is not callable which is easily fixable of course ('build_src.have_pyrex = lambda : True') - leading to: File "/Users/mb312/usr/local/lib/python2.6/site-packages/numpy/distutils/command/build_src.py", line 474, in generate_a_pyrex_source import Pyrex.Compiler.Main ImportError: No module named Pyrex.Compiler.Main I'm afraid I did a rather crude monkey-patch to replace the 'generate_a_pyrex_source' function. It seems to work for numpy 1.1 and current trunk. The patching process is here: http://github.com/matthew-brett/du-cy-numpy/blob/master/matthew_monkey.py Best, Matthew
participants (3)
-
Charles R Harris
-
Kevin Jacobs <jacobs@bioinformed.com>
-
Matthew Brett