我可以在 Cython pxd 文件中使用 cimport 吗?

Can I use cimport inside a Cython pxd file?

我想在 Cython 函数中使用 uint_fast16_t 类型。我可以通过在文件顶部添加 from libc.stdint cimport uint_fast16_t 来在我的 pxd 文件中访问它吗?我搜索了 Cython 文档,但找不到在 pxd 文件中使用 cimport 的任何提及。

是的。似乎将 cimport 行添加到我的 pxd 文件的顶部确实有效。这是我用来演示的一个小测试用例:

test.pxd:

from libc.stdint cimport uint_fast16_t

cdef uint_fast16_t double_it(uint_fast16_t x)

test.pyx:

# cython: language_level=3, boundscheck=False, wraparound=False, cdivision=True
from test cimport double_it, uint_fast16_t

cdef uint_fast16_t double_it(uint_fast16_t x):
    return 2*x

def fast_double(uint_fast16_t x):
    return double_it(x)

cythonize-3.7 -i test.pyx && python

后的结果
>>> import test
>>> test.fast_double(10)
20

我放在 test.pyx 顶部的编译器指令是从我的主项目文件中复制的。我把它放在那里只是为了确保它使用与我已经使用的完全相同的设置。我认为他们中的任何一个都不需要在 .pxd 文件中使用 cimport