[Numpy-discussion] Recommended way to add Cython extension using numpy.distutils?

Matthew Brett matthew.brett@gmail....
Wed Apr 28 14:01:00 CDT 2010


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


More information about the NumPy-Discussion mailing list