使用Cython组织一个包

bwa*_*en2 6 python cython

我有一个包需要Cython来构建其扩展,我试图调整该setup.py文件以简化安装.

一个简单的

pip install git+git://<pkg-repo> 
Run Code Online (Sandbox Code Playgroud)

抛出错误

$ pip install git+https://<pkg-repo>
Downloading/unpacking git+https://<pkg-repo>
  Cloning https://<pkg-repo> to /tmp/pip-nFKHOM-build
  Running setup.py (path:/tmp/pip-nFKHOM-build/setup.py) egg_info for package from git+https://<pkg-repo>
    Traceback (most recent call last):
      File "<string>", line 17, in <module>
      File "/tmp/pip-nFKHOM-build/setup.py", line 2, in <module>
        from Cython.Build import cythonize
    ImportError: No module named Cython.Build
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):

  File "<string>", line 17, in <module>

  File "/tmp/pip-nFKHOM-build/setup.py", line 2, in <module>

    from Cython.Build import cythonize

ImportError: No module named Cython.Build
Run Code Online (Sandbox Code Playgroud)

因为在安装Cython依赖项之前进行了Cython导入.这导致了多阶段安装过程:

pip install <deps> cython
pip install git+git://<pkg-repo>
Run Code Online (Sandbox Code Playgroud)

这太糟糕了.相关部分setup.py是:

from setuptools import setup, find_packages
from Cython.Build import cythonize

setup(
    install_requires=[
        ...
        'cython>=0.19.1'
        ...
    ],
    ext_modules=cythonize([
        ...
        "pkg/io/const.pyx",
        ...
    ])
)
Run Code Online (Sandbox Code Playgroud)

如何setup.py在依赖于install_requires获取Cython的同时更改为仍然对ext_modules 进行cython化?

Mar*_*sos 3

从版本 18.0 开始,setuptools 支持 Cython:您只需在 setup_requires 中指定 cython 版本并在 Extension 中列出所需的源,setuptools 将确保使用 Cython 构建它们并在需要时安装它 - 无需从您的显式cythonize()调用setup.py。

你的 setup.py 看起来像这样:

from setuptools import setup, Extension

setup(
    setup_requires=[
        ...
        'setuptools>=18.0',
        'cython>=0.19.1',
        ...
    ],
    ext_modules=Extension([
        ...
        "pkg/io/const.pyx",
        ...
    ])
)
Run Code Online (Sandbox Code Playgroud)

我也不知道这个功能,直到我发现这个答案中提到了它: https: //stackoverflow.com/a/38057196/1509394