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
:
from foo.bar cimport Bar
cdef class Baz(Bar):
pass
cdef class Baz(Bar):
def __init__(self, *a, **k):
...
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.
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.
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.
Cython code, unlike Python, must be compiled. This happens in two stages: A . pyx file is compiled by Cython to a .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With