【问题标题】:Organizing a package with Cython用 Cython 组织一个包
【发布时间】:2014-09-15 09:02:03
【问题描述】:

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

一个简单的

pip install git+git://<pkg-repo> 

抛出错误

$ 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

因为安装 Cython 依赖项之前的 Cython 导入。这导致了一个多阶段的安装过程:

pip install <deps> cython
pip install git+git://<pkg-repo>

这很糟糕。 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",
        ...
    ])
)

如何更改 setup.py 以仍然对 ext_modules 进行 cythonize,同时依靠 install_requires 首先获得 Cython?

【问题讨论】:

    标签: python cython


    【解决方案1】:

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

    您的 setup.py 将如下所示:

    from setuptools import setup, Extension
    
    setup(
        setup_requires=[
            ...
            'setuptools>=18.0',
            'cython>=0.19.1',
            ...
        ],
        ext_modules=Extension([
            ...
            "pkg/io/const.pyx",
            ...
        ])
    )
    

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

    【讨论】:

      猜你喜欢
      • 2013-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-03
      • 2013-03-28
      • 2021-01-06
      相关资源
      最近更新 更多