Question

I have just managed to build my first C extension for Python, using Cython to call into an existing C library.

I declared and defined my data types and functions into logical components (following the logical structure of the C library), and I combined them into one pyx file - after errors occured when I tried to add the files individually (IIRC I got an error something along the lines of init already defined - and after researching the issue on Google, I found that I had to combine all the pyx files info one pyx file) - see this link.

This is a copy of the contents of my foo.pyx file:

#include "myarray.pyx"
#include "myset.pyx"
#include "mycalc.pyx"

and this is a copy of my setup file:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

setup(
    cmdclass = {'build_ext': build_ext},
    ext_modules = [Extension("foo", ["foo.pyx"],
                                 libraries=["foo_core"])
                  ]
)

The extension gets built succesfully into foo.so I can then type "import foo" at the Python CLI. That also works. However, when I try to access any of the classes I declared/defined in myarray.pxd, myarray.pyx etc, I get the error message:

AttributeError: 'module' object has no attribute 'myArray'

I then tried dir(), to see what the foo module was exporting. To my surprise, this is what it listed:

>>> dir(foo)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__test__']

Why is Cython failing to export the structs, classes and functions I have declared and defined?. I don't think there is anything wrong with my pxd and pyx files because like I said, it compiles succesfully and the shared lib (python extendion) is produced.

I am using Cython 0.15.1 and Python 2.6.5 on Ubuntu

Was it helpful?

Solution

# declares start of a comment line so your foo.pyx is effectively empty.

include is a blunt instrument. Use *.pxd and cimport instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top