[英]Cython- import class to pyx file
For example let's say we have two classe,例如,假设我们有两个班级,
Bus - Implement The physical bus line.总线-实现物理总线。
src/bus/bus.pxd源码/总线/bus.pxd
cdef class Bus:
cdef int get_item(self)
src/bus/bus.pxd:源代码/总线/bus.pxd:
cdef class Bus:
cdef int get_item(self):
return 5
CPU - Implement The physical cpu processor CPU -实现物理 cpu 处理器
src/cpu/cpu.pyx:源代码/cpu/cpu.pyx:
cimport bus.Bus as Bus
cdef class Cpu:
cdef Bus bus
cdef __cinit__(self, Bus bus):
self.bus = bus
setup.py:安装程序.py:
from setuptools import setup, Extension
import sys
import numpy
USE_CYTHON = False
files = [
('src.bus', 'src/bus/bus'),
('src.cpu', 'src/cpu/cpu'),
]
if '--use-cython' in sys.argv:
USE_CYTHON = True
sys.argv.remove('--use-cython')
ext = '.pyx' if USE_CYTHON else '.c'
extensions = []
for package_path, file_path in files:
extensions.append(
Extension(package_path,[f"{file_path}{ext}"],
language='c',
include_dirs=['src/c/'])
)
if USE_CYTHON:
from Cython.Build import cythonize
extensions = cythonize(extensions)
setup(
ext_modules=extensions,
include_dirs=[numpy.get_include()]
)
However, when compiling this example the compiler raise error of 'Bus' is not a type identifier
.但是,在编译此示例时,编译器会引发错误'Bus' is not a type identifier
。 Any suggestion?有什么建议吗?
So, in cython we've to use pxd file for declaration in compile time.因此,在 cython 中,我们必须在编译时使用 pxd 文件进行声明。 Therefor, adding __init__.pxd
have to bee added for compilation import.因此,必须添加__init__.pxd
才能编译导入。 In Addition, I've created cpu.pxd
file which inside I've wrote from bus cimport Bus
.另外,我创建了cpu.pxd
文件,我在里面写了from bus cimport Bus
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.