[Python] Cython - Building package with source and compiled files

Discussão em 'Python' iniciado por Stack, Março 13, 2025.

  1. Stack

    Stack Membro Participativo

    I'm pretty new to Cython. What I'm trying to do, is:

    • Create a Python package with some Cython functions
    • Use that package in a project (that uses Cython and Python)

    The problem that I have is that I want to use the Cython functions (.pxd and .pyx) in the project, but when I install the package, I only have the .so files inside (already compiled).

    Is it possible to combine all three file types (pxy, pxd and so) when downloading the package?

    Thanks!

    So far, I have already:

    • Built functions (with no compilation errors)
    • Created setup.py (with correct cythonize)
    • Imported the package into the project, via pip install -r requirements.txt, using SSH and the link of the repo in Github.

    setup.py:

    from setuptools import setup, Extension, find_packages
    from Cython.Build import cythonize
    from setuptools.command.build_ext import build_ext
    import os
    import glob

    extensions = [
    Extension(
    name=os.path.splitext(os.path.relpath(source, "."))[0].replace(
    os.path.sep, "."
    ),
    sources=[source],
    language="c",
    include_dirs=["test_package"],
    )
    for source in glob.glob("test_package/**/*.pyx", recursive=True)
    if os.path.basename(source) != "__init__.py"
    ]


    setup(
    name="test_package",
    version="1.0.0",
    packages=find_packages(),
    package_data={"test_package": ["**/*.pxd", "**/*.pxy"]},
    include_package_data=True,
    url="https://github.com/package/test_package",
    install_requires=[
    "Cython==3.0.12"
    ],
    setup_requires=["Cython==3.0.12"],
    ext_modules=cythonize(extensions, language_level="3"),
    cmdclass={"build_ext": build_ext},
    python_requires="==3.10",
    )


    project organization:

    .
    ├── MANIFEST.in
    ├── README.md
    ├── test_package
    │ ├── mappings
    │ │ ├── __init__.py
    │ │ ├── mappings.pxd
    │ │ └── mappings.pyx
    │ └── utils
    │ ├── __init__.py
    │ ├── utils.pxd
    │ ├── utils.pyx
    ├── compile.sh
    ├── pyproject.toml
    ├── setup.py

    Continue reading...

Compartilhe esta Página