11

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?
Community
  • 1
  • 1
Thomas Johnson
  • 10,776
  • 18
  • 60
  • 98

1 Answers1

17

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.

Community
  • 1
  • 1
Blake Walsh
  • 1,501
  • 14
  • 10
  • 1
    Is the `.pyxbld` file documented **anywhere**? If it is, I'm failing to find it. – Fake Name Jun 06 '17 at 01:48
  • 1
    Ok. Is it documented anywhere that people actually use on a daily basis (read: somewhere I can find with google et. al)? – Fake Name Jun 06 '17 at 19:41
  • @FakeName Not as far as I know, but it is just plain text and you can read it in the git repository: https://github.com/cython/cython/blob/master/pyximport/pyximport.py or you could build it into a HTML file using a tool like pdoc, on the whole Cython online documentation is haphazard and you often have to go to the source files if you want to figure stuff out – Blake Walsh Jun 07 '17 at 09:45