pxd相对cimport适用于language_level 2,但不适用于3

时间:2019-05-13 15:07:26

标签: python-3.x cython

!一切都适用于language_level = 2,但不适用于language_level = 3

我必须用Cython包装c库,并且我也想复制库的结构以更好地理解。所以我想用pxd文件创建一个单独的文件夹。

项目结构如下:

enter image description here

setup.py:

from setuptools import setup, Extension
from Cython.Build import cythonize
from Cython.Distutils import build_ext
from pathlib import Path

setup_file_directory = Path(__file__).resolve().parent
NAME = 'example'
SRC_DIR = "lib"
PACKAGES = [SRC_DIR]

ext = Extension(name=SRC_DIR + ".wrapper.some_code",
                sources=[SRC_DIR + "/wrapper/some_code.pyx"]
                )

EXTENSIONS = [ext]

if __name__ == "__main__":
    setup(
        packages=PACKAGES,
        zip_safe=False,
        name=NAME,
        cmdclass={"build_ext": build_ext},
        ext_modules=cythonize(EXTENSIONS, language_level=3),
    )

some_code.pyx:

from pxd_include.inc_file cimport *

cdef custom_int return_int(custom_int input_int):
    print(input_int)

inc_file.pxd:

ctypedef int custom_int

在setup.py中使用language_level = 2可以正常工作并编译。如果将其切换为3,则会收到错误消息:

enter image description here

这是由于无法导入language_level = 3的pxd文件引起的。该如何解决?

1 个答案:

答案 0 :(得分:0)

要使相对路径在language_level = 3下工作,我必须以这种方式在somecode.pyx中导入pxd:

from lib.wrapper.pxd_include.inc_file cimport *

{{1}}

最后一个表示法与language_level = 2兼容

相关问题