Ivan Mishalkin
Ivan Mishalkin

Reputation: 1108

pxd relative cimport works for language_level 2 but not for 3

!Everything works with language_level=2, but does not with language_level=3

I have to wrap c-library with Cython and I also want to copy the structure of the library for better understanding. So I'd like to create separate folder with pxd files.

The project structure is following:

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

with language_level=2 in setup.py everything works and compiles. If I switch it to 3 I get an Error:

enter image description here

It is caused by unability to import pxd file with language_level=3. How to fix that?

Upvotes: 2

Views: 540

Answers (1)

Ivan Mishalkin
Ivan Mishalkin

Reputation: 1108

To make relative path work with language_level=3 I had to import pxd in somecode.pyx in this way:

from .pxd_include.inc_file cimport *

or

from lib.wrapper.pxd_include.inc_file cimport *

Last notation is compatible with language_level=2

Upvotes: 2

Related Questions