Built Python package can't find compiled Cython/C extension; works fine through pip install -e

37 views Asked by At

I'm trying to package and distribute some Python code containing Cython and C modules that will be compiled during installation. I can get it to build using python3 -m build but I can't then import the modules after regular installation (e.g. pip3 install dist/*.whl) using import pkg.module_name.

If I cd into the install directory and then try to run import module_name it works fine.

The frustrating thing is that it works just fine importing it like import pkg.module_name if I install the package using pip3 install -e ./ instead of building and then installing (though then I obviously can't distribute it!)

What am I missing?


Minimal example folder structure:

.
├── MANIFEST.in
├── pkg
│   ├── c
│   │   └── hash.c
│   ├── fasthash.c
│   ├── __init__.py
│   ├── lemmatize.pyx
│   └── wrapper.py
├── pyproject.toml
└── setup.py

setup.py:

from setuptools import find_packages, setup, Extension
from Cython.Build import build_ext

setup(
    name="pkg",
    packages=find_packages(),
    ext_modules=[
        Extension(
            name="pkg.lemmatize",
            sources=["pkg/lemmatize.pyx"],
        ),
        Extension(
            name="pkg.fasthash",
            sources=["pkg/fasthash.c"],
            extra_compile_args=["-Wall"],
        )
    ],
    cmdclass={'build_ext': build_ext},
)

pyproject.toml:

[project]
name = "pkg"
version = "0.0.0"
dependencies = ["cython<3.0.0",]

[build-system]
requires = [
    "wheel",
    "setuptools",
    "cython<3.0.0",
]
build-backend = "setuptools.build_meta"

MANIFEST.in:

recursive-include pkg *.pyx
recursive-include pkg/c *.c

And then the folder where pip installs it to (e.g. $VENV/lib/python3.11/site-packages/pkg) after installation:

.
├── c
│   └── hash.c
├── fasthash.c
├── fasthash.cpython-311-x86_64-linux-gnu.so
├── __init__.py
├── lemmatize.cpython-311-x86_64-linux-gnu.so
├── lemmatize.pyx
└── wrapper.py
0

There are 0 answers