4

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?

0 _
  • 10,524
  • 11
  • 77
  • 109
Eli
  • 36,793
  • 40
  • 144
  • 207
  • Relevant: https://stackoverflow.com/questions/56115159/pxd-relative-cimport-works-for-language-level-2-but-not-for-3 – 0 _ Jun 30 '21 at 03:37
  • Relevant: https://stackoverflow.com/questions/58083432/how-to-import-depending-cython-modules-from-parent-folder-in-python – 0 _ Jun 30 '21 at 03:39
  • Relevant: https://stackoverflow.com/questions/33555927/cython-relative-cimport-beyond-main-package-is-not-allowed – 0 _ Jun 30 '21 at 03:40

2 Answers2

1

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 _
  • 10,524
  • 11
  • 77
  • 109
Eli
  • 36,793
  • 40
  • 144
  • 207
0

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

user985030
  • 1,557
  • 1
  • 16
  • 32