导入cython函数:AttributeError:'模块'对象没有属性'fun' [英] Importing cython function: AttributeError: 'module' object has no attribute 'fun'

查看:121
本文介绍了导入cython函数:AttributeError:'模块'对象没有属性'fun'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个小的 cython 代码

 #来自libc.stdlib的t3.pyx 
cimport atoi

cdef int fun(char * s):
返回atoi(s)

setup.py 文件是



<来自distutils.core的pre> 来自Cython的导入设置
。构建导入cythonize

setup(ext_modules = cythonize( t3.pyx))

我使用此命令运行 setup.py

  python setup.py build_ext --inplace 

这给了我

 编译t3.pyx,因为它已更改。 
Cythonizing t3.pyx
运行build_ext
构建't3'扩展名
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:警告:已定义'__pyx_f_2t3_fun'但未使用[-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原型-D_FORTIFY_SOURCE = 2 -g -fstack-protector-strong -Wformat -Werror = format-security -Wl,-符号功能-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

何时我在 python 解释器中运行,它向我显示

 > > import t3 
>> t3.fun(’1234’)
追溯(最近一次通话为最后):
文件< stdin>,在< module>中的第1行。
AttributeError:模块对象没有属性有趣
>>>


解决方案

这里的问题是您使用 cdef 而不是 def cdef 方法只能从 cython 代码中调用。



您可以在 Python函数与文档的C函数部分。


Python函数是使用def语句定义的,就像在Python中一样。
它们将Python对象作为参数并返回Python对象。



C函数是使用新的cdef语句定义的。它们采用
Python对象或C值作为参数,并且可以返回Python
对象或C值。


和重要的部分:


在Cython模块中,Python函数和C函数可以自由调用彼此的
,但是解释后的Python代码只能从
模块外部调用Python函数。因此,您要使用Cython模块从$ c $ b中导出的任何函数必须使用def声明为Python函数



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 setup
from Cython.Build import cythonize

setup(ext_modules=cythonize("t3.pyx"))

I run setup.py using this command

python setup.py build_ext --inplace

This gives me

Compiling t3.pyx because it 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 but 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 I run in the python interpreter it 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 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.

这篇关于导入cython函数:AttributeError:'模块'对象没有属性'fun'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆