[Python] Cython *.pxd file cannot use absolute imports for Cython submodule

Discussão em 'Python' iniciado por Stack, Maio 14, 2025.

  1. Stack

    Stack Membro Participativo

    I am getting an Error compiling Cython file, 'myproject/utils/hdf5.pxd' not found` when trying to build my Cython project in a certain way.

    My Cython project is organized this way:

    myproject/
    setup.cfg
    setup.py <-- For installing the Python project

    myproject/
    __init__.py
    reco.pyx
    resample.pxd
    resample.pyx
    setup.py <-- For building the Cython module

    utils/
    __init__.py
    hdf5.pxd
    hdf5.pyx
    ...
    setup.py <-- For building the Cython (sub-)module


    In myproject/myproject/setup.py (for building the Cython module), I have:

    from distutils.core import setup, Extension
    from Cython.Build import cythonize

    reco = Extension(
    name = 'reco',
    sources = ['reco.pyx'],
    include_dirs = ['.', './utils', *HDF5_DIRS],
    ...
    )

    resample = Extension(
    name = 'resample',
    sources = ['resample.pyx'],
    include_dirs = ['.', './utils', *HDF5_DIRS],
    ....

    setup(
    name = 'myproject',
    packages = ['myproject', 'myproject.utils'],
    ext_modules = cythonize([resample, reco]))


    Inside resample.pxd:

    # cython: language_level=3

    import numpy as np
    from myproject.utils.hdf5 cimport hid_t, hsize_t, ...
    ...

    cdef inline hid_t write_resampled(...
    ...


    resample.pyx is empty and contains only the Cython language_level declaration.

    When I exclude the resample module, this project builds and runs just fine. However, when I try to build it including the resample module, I get several errors related to my imports within resample.pxd; here's one example

    python3 setup.py build_ext -if
    Compiling resample.pyx because it changed.
    [1/1] Cythonizing resample.pyx

    Error compiling Cython file:
    ------------------------------------------------------------
    ...
    import numpy as np
    from l4cython.utils.hdf5 cimport hid_t, hsize_t
    ^
    ------------------------------------------------------------

    ./resample.pxd:23:0: 'myproject/utils/hdf5.pxd' not found


    This error surprises me because the reco.pyx file contains identical imports, and the reco modules builds just fine.

    # Inside reco.pyx

    ...
    from l4cython.utils.hdf5 cimport H5T_STD_U8LE, hid_t, read_hdf5, close_hdf5
    ...


    Further, it seems that if I change the problematic imports in resample.pxd to relative imports, everything works:

    # Inside resample.pxd

    # Doesn't work:
    # from myproject.utils.hdf5 cimport hid_t, hsize_t

    # Works:
    from utils.hdf5 cimport hid_t, hsize_t


    Is there a reason why I can use an absolute cimport from a Cython sub-module in my .pyx file but not in my .pxd file? The C-defined functions in my .pxd file are re-used throughout the program, so I definitely want to have them available for re-use (i.e., not have to copy them to every .pyx file that needs them). However, they depend, in turn, on definitions from other .pxd files.

    Continue reading...

Compartilhe esta Página