Hello cython community,
I am struggling with sharing a set of cython (cdef-) functions between cython modules.
Unfortunately, the ressources I can find for this are rather sparse/only cover the most basic examples.
Briefly, the project structure, because I assume it might matter:
root folder / has setup.py
then
/folder0/folder1
contains a bunch of cython extensions which are compiled (in that same folder1 / module) when running setup.py
extension0.pyx
extension1.pyx
_shared_ct.pyx
Specifically, I the latter file, _shared_ct.pyx contains a bunch of functions I want the other modules to have access to.
An example of the typical signatures:
cdef np.ndarray[np.float64_t, ndim=3] example_func(np.ndarray[np.float64_t, ndim=1] a, double[:,:,::1] b, int c, int d, int [:,::1] e, int f, bint g):
cdef np.ndarray[np.float64_t, ndim=3] some_array
# ... do lots of good stuff ...
return some_array
I understand I need a _shared_ct.pxd file that contains the function signatures for that. So I have a file, _shared_ct.pxd also in /folder0/folder1, which has:
cimport numpy as np
# <- first question: is this correct? I assume I need something of this sort, but not sure
# (I couldn't get it work with any variation - with or without import / cimport numpy in the .pxd - I could think of)
and then for for each function in _shared_ct.pyx:
cdef np.ndarray[np.float64_t, ndim=3] example_func(np.ndarray[np.float64_t, ndim=1], double[:,:,::1], int , int , int [:,::1], int, bint)
i.e. the function with return and argument types, but argument names, body etc removed.
and then for example in extension0.pyx:
from _shared_ct cimport example_func
... but when I run setup.py to build the extensions , one of two things will happen:
a) it will refuse to compile at all; the error message states " _ct_shared.pxd not found"
- this is what happened first
b) if I add e.g. "folder0/folder1" to the include_dirs of module0 in setup.py it appears to compile ok, but then when I import
extension0, it will raise "_shared_ct not found"
I'm a bit lost what I should do.
Do I need to put _shared_ct as an extension in setup.py as well and compile it first for this to work? That actually would make sense to me, sense, but I never found any mention that this should be necessary, so I'm unsure how to proceed.
Any help appreciated.
(on a sidenote, if I instead
include "_shared_ct.pyx"
in the other modules, all works well, so that's a workaround - but it seems less clean, and apparently can lead to lots of hard to spot problems when recompiling, so I'd rather avoid that if possible.)
Thanks,
David