我有一个Cython .pxd文件和一个Cython .pyx文件,pyx文件包含一个cdef类:
# myclass.pyx (compiled to myclass.so)
cdef class myclass:
pass现在是另一个特性的.pxd文件
# another.pxd (with another.pyx along)
from libcpp.vector cimport vector
import myclass # This line is funny, change 'myclass' to whatever and no syntax error
cdef vector[myclass] myvar # COMPILE TIME ERROR
cdef myclass myvar2 # SIMPLER, STILL COMPILE TIME ERROR在编译another.pyx时,Cython显示关于vector[myclass]的错误,它说'myclass‘是未知的。为什么会这样呢?
发布于 2021-11-07 06:06:10
应该以这样的方式清楚地描述:
'import'
导入
问题是import myclass in another.pxd不会导入myclass,因为它是cdef (Cython )。
在another.pxd文件中,要导入“myclass”,它必须是:
from myclass cimport myclass cdef myclass myvar2cimport myclass cdef myclass.myclass myvar2cimport some.path.to.myclass as myclass cdef myclass myvar2导出可能也有问题,特别是使用Python工具而不是直接使用cython3和gcc:
myclass不是公共的,导入后看不到
因此,最好将myclass置于myclass.pyx中的公共范围内:
cdef public:
class myclass:
passhttps://stackoverflow.com/questions/69766466
复制相似问题