Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set Cython compiler flags when using pyximport?

Tags:

python

cython

This question (How does one overwrite the default compile flags for Cython when building with distutils?) describes how to set default Cython flags when using distutils.

But how do I set default compile flags if I'm just using pyximport?

import pyximport
pyximport.install()  # Pass compile flags here somehow?
like image 366
Thomas Johnson Avatar asked Nov 09 '14 22:11

Thomas Johnson


Video Answer


1 Answers

You should use a .pyxbld file, see for example this question. For a file named foo.pyx, you would make a foo.pyxbld file. The following would give extra optimization args:

def make_ext(modname, pyxfilename):
    from distutils.extension import Extension
    return Extension(name=modname,
                     sources=[pyxfilename],
                     extra_compile_args=['-O3', '-march=native'])

I think it might be possible to pass in extra setup options to pyximport.install if you jump through enough hoops (messing around with distribute) to get the setup_args in the form it likes, however in the pyximport module documentation it recommends using a .pyxbld file, and in the test code for pyximport only that method is tested, so if there is another way it should be considered unstable/untested and .pyxbld should be considered the proper way of doing this.

like image 67
Blake Walsh Avatar answered Oct 29 '22 06:10

Blake Walsh