【问题标题】:Create package with cython so users can install it without having cython already installed使用 cython 创建包,以便用户可以在没有安装 cython 的情况下安装它
【发布时间】:2017-10-17 07:41:31
【问题描述】:

我有一个问题。我想分发我的 cython 驱动的包,但我看不到在 setup.py 中构建它们的简单方法。我希望 setup.py :

  • 最重要的是:在没有 cython 的情况下安装我的包(从预生成的 C 文件或预先安装 cython)
  • 在 sdist 上重建(运行 cythonize)包
  • 不需要对我的 cython 模块列表进行硬编码(只需使用 glob 或其他东西)
  • 能够在没有 .c 文件(不应存储在 git 中)或 .pyx(可能不分发)的情况下工作。当然,其中至少有一套会一直存在。

目前在我的痒包中,我正在使用这个相当复杂的代码:

import os
from glob import glob
from distutils.command.build_ext import build_ext as _build_ext
from distutils.command.sdist import sdist as _sdist
from distutils.core import setup
from distutils.core import Extension



def generate_extensions():
    return [
        # Compile cython-generated .c files into importable .so libraries.
        Extension(os.path.splitext(name)[0], [name])
        for name in C_FILES
    ]


# In distribution version, there are no pyx files, when you clone package from git, there will be no c files.
CYTHON_FILES = glob('itchy/*.pyx')
C_FILES = glob('itchy/*.c')
extensions = generate_extensions()


class build_ext(_build_ext):
    def run(self):
        # Compile cython files (.pyx, some of the .py) into .c files if Cython is available.
        try:
            from Cython.Build import cythonize
            if CYTHON_FILES:
                cythonize(CYTHON_FILES)

                # Update C_FILES in case they were originally missing.
                global C_FILES, extensions
                C_FILES = glob('itchy/*.c')
                extensions = generate_extensions()
            else:
                print('No .pyx files found, building extensions skipped. Pre-built versions will be used.')
        except ImportError:
            print('Cython is not installed, building extensions skipped. Pre-built versions will be used.')
            assert C_FILES, 'C files have to be present in distribution or Cython has to be installed'
        _build_ext.run(self)


class sdist(_sdist):
    def run(self):
        # Make sure the compiled Cython files in the distribution are up-to-date
        self.run_command("build_ext")
        _sdist.run(self)


setup(
    (...)
    ext_modules = extensions,
    cmdclass = {
        'build_ext': build_ext,
        'sdist': sdist,
    },
)

【问题讨论】:

    标签: python cython distutils


    【解决方案1】:

    通常通过尝试导入 cython 并调整扩展来完成

    1. 如果存在 cython,则使用 cython 构建 pyx 文件
    2. 如果 cython 不存在,则构建 C 文件

    例如:

    try:
        from Cython.Distutils.extension import Extension
        from Cython.Distutils import build_ext
    except ImportError:
        from setuptools import Extension
        USING_CYTHON = False
    else:
        USING_CYTHON = True
    
    ext = 'pyx' if USING_CYTHON else 'c'
    sources = glob('my_module/*.%s' % (ext,))
    extensions = [
        Extension(source.split('.')[0].replace(os.path.sep, '.'),
                  sources=[source],
        )
    for source in sources]
    cmdclass = {'build_ext': build_ext} if USING_CYTHON else {}
    
    setup(<..>, ext_modules=extensions, cmdclass=cmdclass)
    

    需要 source.split 的东西,因为 cythonized 扩展名需要采用 my_module.ext 的形式,而 glob 需要像 my_module/ext 这样的路径名。

    See this repository 是一个真实的例子。

    但是,您应该在您的 git 存储库以及可分发的文件中包含 .c 文件,否则在构建分发时,.c 文件将被重新构建,并且可能是也可能不是相同的文件就像在您的机器上构建的那样。

    它们可能由其他版本的 cython 构建,例如,或在不同的平台上生成不同的代码。

    Cython 是一个静态编译器 - 建议将其生成的文件提交到存储库。

    强烈建议您分发生成的 .c 文件以及 Cython 源代码,以便用户无需 Cython 即可安装您的模块。

    See Cython documentation on distributing modules.

    【讨论】:

    • 我不同意将 cython 生成的 C 文件存储在您的 repo 中是个好主意,尤其是在 cython 代码经常更改的情况下。相反,您可以让 .c 文件在您发布时包含在您的源代码分发中,但如果他们从 repo 构建项目,仍然要求开发人员从 cython 生成 C 文件。这样可以避免自动生成的文件中不断的代码搅动。
    • 分发不受修订控制的源代码文件是一个主要的禁忌。特别是因为有很多方法可以生成与用于测试的不同的 C 文件,例如通过使用不同的 cython 版本或在不同平台上进行的 cython 编译。见documentation。
    • 对我来说,在 repo 中包含 C 文件似乎与确保构建一致的目标背道而驰。它会引入第二个事实来源,并且会阻止集中式自动构建环境,因为部分构建将是手动的并且在开发人员的机器上,因此完整的构建环境将取决于最后一次提交的人。
    • 在这种情况下,生成的 C 文件实际上不是源代码,因为它们不是目标的源(.pyx 文件是源)。如果您希望用户拥有相同的编译目标,那么您应该分发目标(例如作为轮子)
    • @danny 我想在你的 C 项目中你提交所有生成的汇编代码,对吧?否则,如果开发人员在 gcc/icc/clang 之间切换,您如何保证构建的一致性?如果这听起来很傻,那么关于您在 Cython 项目中提交生成的 C 文件的建议也可以这样说。官方文档实际上建议分发 C 文件:这意味着在 PyPI 的源代码分发中。它并没有说您应该将生成的 C 文件放在 git 存储库中,因为这简直是个坏主意。
    猜你喜欢
    • 1970-01-01
    • 2015-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 2016-05-21
    相关资源
    最近更新 更多