python - Importing cython function: AttributeError: 'module' object has no attribute 'fun' -
i have written small cython
code is
#t3.pyx libc.stdlib cimport atoi cdef int fun(char *s): return atoi(s)
the setup.py
file is
from distutils.core import setup cython.build import cythonize setup(ext_modules=cythonize("t3.pyx"))
i run setup.py
using command
python setup.py build_ext --inplace
this gives me
compiling t3.pyx because changed. cythonizing t3.pyx running build_ext building 't3' extension x86_64-linux-gnu-gcc -pthread -dndebug -g -fwrapv -o2 -wall -wstrict- prototypes -fno-strict-aliasing -d_fortify_source=2 -g -fstack-protector- strong -wformat -werror=format-security -fpic -i/usr/include/python2.7 -c t3.c -o build/temp.linux-x86_64-2.7/t3.o t3.c:556:12: warning: ‘__pyx_f_2t3_fun’ defined not used [-wunused-function] static int __pyx_f_2t3_fun(char *__pyx_v_s) { ^ x86_64-linux-gnu-gcc -pthread -shared -wl,-o1 -wl,-bsymbolic-functions -wl,-bsymbolic-functions -wl,-z,relro -fno-strict-aliasing -dndebug -g -fwrapv -o2 -wall -wstrict-prototypes -d_fortify_source=2 -g -fstack-protector-strong -wformat -werror=format-security -wl,-bsymbolic-functions -wl,-z,relro -d_fortify_source=2 -g -fstack-protector-strong -wformat -werror=format-security build/temp.linux-x86_64-2.7/t3.o -o /home/debesh/documents/cython/t3/t3.so
when run in python
interpreter shows me
>>> import t3 >>> t3.fun('1234') traceback (most recent call last): file "<stdin>", line 1, in <module> attributeerror: 'module' object has no attribute 'fun' >>>
the problem here defined method cdef
instead of def
. cdef
methods can called cython
code.
you can find detailed information in python functions vs. c functions section of docs.
python functions defined using def statement, in python. take python objects parameters , return python objects.
c functions defined using new cdef statement. take either python objects or c values parameters, , can return either python objects or c values.
and important part:
within cython module, python functions , c functions can call each other freely, python functions can called outside module interpreted python code. so, functions want “export” cython module must declared python functions using def.
Comments
Post a Comment