Skip to content Skip to sidebar Skip to footer

Importing Cython Function: AttributeError: 'module' Object Has No Attribute 'fun'

I have written a small cython code that is #t3.pyx from libc.stdlib cimport atoi cdef int fun(char *s): return atoi(s) the setup.py file is from distutils.core import set

Solution 1:

The problem here is that you defined your method with cdef instead of def. cdef methods can only be called from cython code.

You can find detailed information in the Python functions vs. C functions section of the docs.

Python functions are defined using the def statement, as in Python. They take Python objects as parameters and return Python objects.

C functions are defined using the new cdef statement. They take either Python objects or C values as parameters, and can return either Python objects or C values.

and the important part:

Within a Cython module, Python functions and C functions can call each other freely, but only Python functions can be called from outside the module by interpreted Python code. So, any functions that you want to “export” from your Cython module must be declared as Python functions using def.


Post a Comment for "Importing Cython Function: AttributeError: 'module' Object Has No Attribute 'fun'"