Search code examples
pythoncythonsetuptoolspython-packagingpython-poetry

Cython generated c/cpp files in poetry sdist tar.gz for no-cython-installation


I use Poetry to build tar.gz. and .whl of my package. Cython docs recommend to distribute cython generated c files along with pyx ones. http://docs.cython.org/en/latest/src/userguide/source_files_and_compilation.html#distributing-cython-modules

What should I add to build.py or pyproject.toml to generate c/cpp files by calling poetry build and poetry build -f sdist?

I tried this (from Create package with cython so users can install it without having cython already installed):

build.py:

from setuptools.command.build_ext import build_ext
from setuptools.command.sdist import sdist as _sdist
...
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)

def build(setup_kwargs):
    setup_kwargs.update({
        ...
        'cmdclass': {'sdist': sdist,
                     'build_ext': build_ext}
    })

Not worked for me.


Solution

  • The current version of poetry (1.0.5) ignores custom build.py when building an sdist, so there's no chance without modifying poetry first. In the meantime, you can use third-party projects like taskipy to replace the poetry build command with a custom one, e.g.

    # pyproject.toml
    
    ...
    
    [tool.poetry.dev-dependencies]
    cython = "^0.29.15"
    taskipy = "^1.1.3"
    
    [tool.taskipy.tasks]
    sdist = "cython fib.pyx && poetry build -f sdist"
    
    ...
    

    and execute poetry run task sdist instead of poetry build -f sdist.