pythonpipcythonsetuptoolscythonize

UserWarning: Unknown Extension options: 'cython_directives' when locally installing Cython package


I am using Cython version 0.29.26. I have a python package with a Cython extension as follows:

./setup.py:

from setuptools import setup, find_packages, Extension
from setuptools.command.build_ext import build_ext
import numpy as np


cython_directives = {'boundscheck': "False",
                     'cdivision': "True",
                     'wraparound': "False",
                     'language_level': "3"}

setup(name='debug_example',
      packages=find_packages(),
      cmdclass={'build_ext': build_ext},
      ext_modules=[
          Extension('debug_example.example',
                    sources=['debug_example/example.pyx'],
                    language='c++',
                    include_dirs=[np.get_include()],
                    cython_directives=cython_directives,
                    )
      ]
      )

./debug_example/example.pyx

cimport numpy as  np

cpdef int increment(int a):
    return a + 1

./debug_example/__init__.py:

from .example import increment

When I compile it, I get a warning message about cython_directives being unknown (compilation works fine apart from that). Output of pip install -e . -v:

(base) ➜  debug_cython pip install -e . -v
Using pip 21.2.4 from /home/mm/anaconda3/lib/python3.9/site-packages/pip (python 3.9)
Obtaining file:///home/mm/tmp/debug_cython
    Running command python setup.py egg_info
    /home/mm/anaconda3/lib/python3.9/site-packages/setuptools/_distutils/extension.py:131: UserWarning: Unknown Extension options: 'cython_directives'

what is the cause of this ?


Solution

  • Extension is from setuptools which has somewhat limited support for Cython: it automatically invokes cythonize for *.pyx-files but for more options one should use cythonize directly. That means the following for your setup.py:

    ...
    from Cython.Build import cythonize
    
    ...
    extensions = [Extension('debug_example.example',
                        sources=['debug_example/example.pyx'],
                        language='c++',
                        include_dirs=[np.get_include()],
                        ),
                  ]
    ...
    
    setup(...
          ext_modules = cythonize(extensions, compiler_directives=cython_directives)
          ...
          )
    

    I.e. compiler_directives can be passed to cythonize.