Skip to content Skip to sidebar Skip to footer

Building Python Package Containing Multiple Cython Extensions

I have the following directory structure: testcython/ setup.py testcython/ __init__.py foo.pyx stuff.py bar/ __init__.pxd

Solution 1:

I resolved this issue, with the help of a colleague, so I'll mention the solution here in case it helps people in the future.

The problem is related to how the Cython modules are imported, and more specifically - where the .so file is placed upon building the extension. Originally, the Bar.so file was generated in the testcython directory - such that when attempting import from the bar sub-module, it couldn't find the corresponding shared object file.

To solve this I needed to use the name "bar.bar" when creating this extension, this then results in the .so file being generated into the testcython/bar directory. Then, in foo.pyx, to use members from this bar module the import had to be changed to from testcython.bar.bar cimport <name>.

Note:

Additionally, the function square shown in the question cannot be used from another Cython module in this form as no __pyx_capi__ is generated for free cdef functions. Instead, this function has to be wrapped in some cdef class as a static method in order to use it from another Cython module, i.e.:

cdef classSquare:
    @staticmethod
    cdef intsquare(int x)

Then this can be imported, in foo.pyx for example, with from testcython.bar.bar cimport Square. The class Square then essentially acts like a "namespace".

Post a Comment for "Building Python Package Containing Multiple Cython Extensions"