使用 setuptools,“可选”C 扩展模块错误是致命的

dst*_*erg 6 python setuptools

我花了一点时间让我的 treap 模块(如字典,但总是按键排序)与 setuptools 39.0.1 一起使用。

我希望它能够从包含的 .c 文件编译并安装 Cython 版本,或者在失败时退回到纯 python 版本。

我目前正在使用:

setup(
    name='treap',
    py_modules=[
        'treap',
        'py_treap',
        'nest',
        ],
    # ext_modules=cythonize("pyx_treap.pyx"),
    ext_modules=[Extension('pyx_treap', ['pyx_treap.c'], optional=True)],
    # setup_requires=[
    #     'Cython'
    # ],
    version=version,
    description='Python implementation of treaps,
)
Run Code Online (Sandbox Code Playgroud)

...加上一些似乎不相关的设置选项。

如果我运行 python3.6 setup.py build,我得到:

running build
running build_py
creating build
creating build/lib.linux-x86_64-3.6
copying treap.py -> build/lib.linux-x86_64-3.6
copying py_treap.py -> build/lib.linux-x86_64-3.6
copying nest.py -> build/lib.linux-x86_64-3.6
running build_ext
building 'pyx_treap' extension
creating build/temp.linux-x86_64-3.6
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -g
-fstack-protector-strong -Wformat -Werror=format-security -Wdate-time
-D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.6m -c pyx_treap.c -o
build/temp.linux-x86_64-3.6/pyx_treap.o
pyx_treap.c:4:10: fatal error: Python.h: No such file or directory
 #include "Python.h"
          ^~~~~~~~~~
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
Run Code Online (Sandbox Code Playgroud)

因此,尽管使用 Extension('pyx_treap', ['pyx_treap.c'], optional=True) ,但 cython 版本不是可选的。

我错过了什么吗?为什么不继续使用纯 python 模块呢?

我意识到我可以安装 python3.6-dev 或类似的东西来消除错误,但我不想假设用户知道这一点。

谢谢!