I'm attempting to build a Python C extension using Cython. The problem is that when I structure the project in submodules, the defined objects in Cython are no longer available.
This is a simple project that I created to reproduce the issue I'm having:
├── foo
│ ├── bar
│ │ ├── bar.pyx
│ │ ├── cbar.pxd
│ │ ├── __init__.py
│ ├── __init__.py
├── setup.py
└── src
├── bar.c
└── bar.h
And this is the setup.py:
from setuptools import Extension, setup
from Cython.Build import cythonize
ext_modules = [
Extension("foo.bar", ["foo/bar/bar.pyx"]),
]
setup(
ext_modules=cythonize(ext_modules, language_level=3),
packages=["foo", "foo.bar"]
)
The pyx file is quite simple
# distutils: sources = src/bar.c
# distutils: include_dirs = src/
cimport foo.bar.cbar as s
cdef class Foobar:
cdef int number
def __cinit__(self):
self.number = 357
def double(self):
return s.double_int(self.number)
But when installing with
python setup.py build_ext --inplace
and running a shell to import and try it:
>>> import foo
>>> dir(foo)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'bar', 'foo']
>>> dir(foo.bar)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
>>>
There's no Foobar class available in foo.bar. Also, I don't really know why "foo" is a submodule of itself. Would appreciate any help! Thanks!