用户警告:未知扩展选项:“cython_directives”本地安装 Cython 包时

发布于 2025-01-11 06:37:23 字数 1713 浏览 0 评论 0原文

我正在使用 Cython 版本 0.29.26。 我有一个带有 Cython 扩展的 python 包,如下所示:

./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

当我编译它时,我收到一条关于 cython_directives 未知的警告消息(除此之外,编译工作正常)。 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'

这是什么原因?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挥剑断情 2025-01-18 06:37:23

Extension 来自 setuptools,它对 Cython 的支持有些有限:它会自动为 *.pyx 文件调用 cythonize但对于更多选项,应该直接使用 cythonize 。这意味着您的 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)
      ...
      )

即可以将compiler_directives 传递给 cythonize。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文