【问题标题】:Marking Cython as a Build Dependency?将 Cython 标记为构建依赖项?
【发布时间】:2013-08-04 02:48:12
【问题描述】:

有一个带有 setup.py 的 Python 包,其内容如下:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

setup(
  name = 'fastahack',
  ext_modules=[
    Extension("fastahack.cfastahack",
              sources=["fastahack/cfastahack.pyx", "lib/Fasta.cpp", "lib/split.cpp"],
              libraries=["stdc++"],
              include_dirs=["lib/"],
              language="c++"),
    ],
    package_data = {'lib': ['*.pyx', "*.c", "*.h", "README.rst"]},
    package_dir = {"fastahack": "fastahack"},
    cmdclass = {'build_ext': build_ext},
    packages = ['fastahack', 'fastahack.tests'],
    author = "Brent Pedersen",
    author_email="bpederse@gmail.com",
    #test_suite='nose.collector'
)

如果未安装 Cython,则无法导入此 setup.py。据我所知,导入 setup.py 是 pip 等工具找出包的依赖关系的方式。我想设置这个包,以便它可以上传到 PyPI,并指出它依赖于 Cython,以便在您尝试“pip install fastahack”或尝试“ pip install”直接从 Git 存储库。

如何打包这个模块,以便在未安装 Cython 时从 Internet 正确安装?始终使用最新版本的 Cython 将是一个加分项。

【问题讨论】:

    标签: python pip cython distutils pypi


    【解决方案1】:

    我的 setup.py 标准模板:

    have_cython = 假 尝试: 从 Cython.Distutils 导入 build_ext 作为 _build_ext have_cython = 真 导入错误除外: 从 distutils.command.build_ext 导入 build_ext 作为 _build_ext 如果有_cython: foo = Extension('foo', ['src/foo.pyx']) 别的: foo = Extension('foo', ['src/foo.c']) 设置 ( ... ext_modules=[foo], cmdclass={'build_ext': build_ext}

    并且不要忘记提供带有扩展包的 .c 文件 - 这将允许用户在不安装 cython 的情况下构建模块。

    【讨论】:

    • 不过,这并不是真正的要求;目标是自动安装 cython 作为依赖项...
    • @SamB,你有关于将 cython 作为依赖项的任何更新吗?
    • @zyxue:不,我也没有找到办法。
    【解决方案2】:

    您可以使用 PEP-518 项目规范将 Cython 指定为构建依赖项。

    在文件pyproject.toml(与setup.py在同一目录下)插入:

    [build-system]
    requires = ["setuptools", "wheel", "Cython"]
    

    然后将在构建您的包之前安装 Cython。

    请注意,(当前)如果您在本地将软件包安装为可编辑(即使用--editable-e),则需要将--no-use-pep517 传递给pip install

    【讨论】:

      【解决方案3】:

      Cython 导入使用tryexcept,并根据导入是否成功修改setup。查看 Pandas 的 setup.py 以获得 example

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-12
        • 2016-09-16
        • 2013-01-30
        • 1970-01-01
        • 1970-01-01
        • 2013-01-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多