[Cython] distutils extension pxd problem

Stefan Behnel stefan_ml at behnel.de
Thu Feb 2 08:11:16 CET 2012


Dimitri Tcaciuc, 02.02.2012 01:53:
> I bumped into an issue where my .pyx file doesn't see its matching
> .pxd file. Here's a build test to show the problem If I change my
> target package from `b.a` to just `a`, it works as expected. Running
> `cython src/a.pyx` works as expected as well, but not the Extension.
> 
> ----
> 
> PYTHON setup.py build_ext --inplace
> PYTHON -c "from b import a"
> 
> ######## setup.py ########
> 
> from distutils.core import setup
> from distutils.extension import Extension
> from Cython.Distutils import build_ext
> 
> ext_modules = [
>     Extension("b.a", ["src/a.pyx"])
> ]
> 
> setup(
>     cmdclass = {'build_ext': build_ext},
>     ext_modules = ext_modules
> )
> 
> ######## b/__init__.py ########
> 
> ######## src/a.pxd ########
> 
> cdef class X:
>     cdef object foo
> 
> ######## src/a.pyx ########
> 
> cdef class X:
> 
>     def __cinit__(self):
>         self.foo = 1
> 
> x = X()
> 
> ----
> 
> Traceback (most recent call last):
>   File "<string>", line 1, in <module>
>   File "a.pyx", line 7, in init b.a (src/a.c:793)
>   File "a.pyx", line 5, in b.a.X.__cinit__ (src/a.c:488)
> AttributeError: 'b.a.X' object has no attribute 'foo'
> 
> ----

Any reason you cannot rename "src" to "b"? Because that would fix your
problem. Cython basically uses the same algorithm for finding package
content as Python, i.e. it will look inside the package "b" when looking
for "b.a". And "a.pxd" is not in "b" in your setup.

Stefan


More information about the cython-devel mailing list