Why doesn't my Cython cimport for a pxd file work?

2.5k Views Asked by At

I'm new to Cython, so I might be missing something obvious, but I've read through the documentation and been banging my head against this for a while.

I have a *.pyx file that I build using a setup.py file as follows:

from distutils.core import setup
from Cython.Build import cythonize

setup(
    ext_modules=cythonize("path/to/helpers.pyx"),
)

That works fine, and I can now import path.to.helpers from sister directories and subdirectories without any issues.

Now, recently I decided I want to add a helpers.pxd file as well, so I can cimport parts of it to other Cython modules. I've added a helpers.pxd in path/to to facilitate this, but when I try to cimport path.to.helpers, I get

path/to/helpers.pxd not found

errors. Do I need to change something in my setup.py file to allow cimporting from the *.pxd file?

None of the docs I've found say anything about doing this, and I feel like I've tried everything without any luck. Ideas?

2

There are 2 best solutions below

0
On

Finally fixed this after hours of banging my head against it. *.pxd files are much more finicky about path than *.pyx files, it turns out. I have no idea why. Anyway, just adding the path to my *.pxd file fixed the problem. My setup.py for path/to/helpers.* stays the same, but whenever I'm using any other *.pyx files that need access to path.to.helpers, I make sure the path to path/to/helpers is included as an include-dir, like:

cython *.pyx *.pxd -a --cplus --include-dir ../some/path

0
On

Try adding a __init__.py to your path/to/ directory. That works with distutils I believe.