Search code examples
cython

How to set the compiler options in setup.py


I am trying to compile a simple test.pyx file. To do this I made setup.py as follows:

from setuptools import setup
from Cython.Build import cythonize

setup(
    compiler_directives={'language_level' : "3"},
    extra_compile_args=['-Ofast', '-march=native'],
    ext_modules = cythonize("test.pyx")
)

I get the warnings:

UserWarning: Unknown distribution option: 'compiler_directives'
  warnings.warn(msg)
Unknown distribution option: 'extra_compile_args'
  warnings.warn(msg)

How should I have done this?

I am using Cython version 0.29.35 .


Solution

  • Thanks to Marijn this compiles without warnings:

    from setuptools import setup
    from Cython.Build import cythonize
    from setuptools.extension import Extension
    
    ext_modules = [
        Extension(
            'test_sum',
            language='c',
            sources=['test.pyx'],  # list of source files
            extra_compile_args=['-Ofast', '-march=native'],  # example extra compiler arguments
        )
    ]
    
    setup(
        name = "test module",
        ext_modules = cythonize(ext_modules, compiler_directives={'language_level' : "3"})
        
    )