警告:我对Cython是个新手。:天 我有以下代码:
我的结构h:
typedef struct dataset{
int lines;
char **tid;
}Dataset;
在myiolib.pyx公司公司名称:
^{pr2}$在我的测试.pyx公司名称:
import myiolib
cdef extern from "my_structs.h":
cdef struct Dataset:
int lines
char **tid
def test():
cdef Dataset D
D = myiolib.readDataset("test.dat")
# Do something...
# Free memory (?!)
在测试.py公司名称:
import mytest
mytest.test()
当我输入:cython-a我的测试.pyx上面写着:“无法将Python对象转换为‘Dataset’, 指向D=myiolib.readDataset("测试数据"). 为什么?我不明白。。。我做错什么了?在
首先,我认为你的小例子很糟糕。不包括
setup.py
或任何其他运行代码的方式。在因此,这里有一个适当的最小示例:
test_python.py
^{pr2}$my_test.pyx
my_library.pyx
my_type.h
此错误包含:
这是因为
cdef
正在使用,因此import
将不允许访问该函数。你也用过cdef
,所以我很惊讶你没有这样做。也许用setup.py
编译并不需要这个;这不会让我感到惊讶。即使这样,您仍然在使用import
,而您应该使用cimport
。在添加
my_library.pxd
:(可选)从
pyx
文件中删除cdef extern
并更改到
而且很管用。在
如果这些技巧不能解决你的问题,请给我一个我可以运行的例子。在
相关问题 更多 >
编程相关推荐