Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i work with pre-compiled libraries in cython?

I'm trying to compile a quick extension for a module that was pre-compiled on install (.pyd). Below is a simplistic example of what i'm trying to do. Given foo.pyd:

baz.pxd

from foo.bar cimport Bar

cdef class Baz(Bar):
    pass

baz.pyx

cdef class Baz(Bar):
    def __init__(self, *a, **k):
        ...

setup.py

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

extensions = [Extension('baz', ['baz.pyx',], libraries=['foo.pyd',])]
setup(name='baz', ext_modules=cythonize(extensions))

I've tried many variations of the above, to no avail.

like image 862
Noob Saibot Avatar asked May 08 '14 00:05

Noob Saibot


People also ask

Can Cython use Python libraries?

Because Cython code compiles to C, it can interact with those libraries directly, and take Python's bottlenecks out of the loop. But NumPy, in particular, works well with Cython.

How do I compile a .PYX file?

To compile the example. pyx file, select Tools | Run setup.py Task command from the main menu. In the Enter setup.py task name type build and select the build_ext task. See Create and run setup.py for more details.

Does Cython need to be compiled?

Cython code, unlike Python, must be compiled. This happens in two stages: A . pyx file is compiled by Cython to a .


1 Answers

cimport is for C/C++ APIs (functions, structs, classes) and reads from .pxd files, which are the Cython counterpart to C/C++ headers. If you don't have a .pxd for the foo library at compile-time, you cannot cimport from it. Python extension modules (.pyd on Windows, .so on Linux) typically don't have C APIs at all: they only contain externally visible symbols that allow the Python module importer to recognize their contents as a Python module.

Also, if you want to get a Python class (even one implemented as an extension type) from a module, you need to import it. I don't think a cdef class is allowed to inherit from such a class, though.

like image 152
Fred Foo Avatar answered Oct 31 '22 01:10

Fred Foo