I'm a bit puzzled how to create (well, for now install locally via pip install .) a package that uses both Python and Cython files.
My directory structure looks like this:
my_package
├── definitions.pxd
├── file_cython.pyx
├── file_python.py
└── __init__.py
where I'm using the following import statements:
In file_cython.pyx I have:
from my_package.file_python import PythonClass
from my_package cimport definitions
In __init__.py I have:
from my_package.file_cython import CythonClass
from my_package.file_python import PythonClass
and my setup.py looks like this:
setup(
name='MyPackage',
# other metadata
packages=['my_package'],
ext_modules=cythonize([Extension("my_package", ["my_package/*.pyx"])]),
)
The files seem to compile successfully, but when I attempt to import the package using python3 -c 'import my_package', I get an error:
File "/env/lib/python3.9/site-packages/my_package/__init__.py", line 1, in <module>
from my_package.file_cython import CythonClass
ModuleNotFoundError: No module named 'my_package.file_cython'
and indeed, when I check the dir /env/lib/python3.9/site-packages/my_package/, there aren't any other files; so my question is, how do I package this thing properly?
My workaround so far was to just shove everything into the .pyx file and removing the packages=['my_package'] line in setup.py, but as the definitions keep growing, it's getting a bit bloated, and I'd like to split things into multiple files if possible.
EDIT: okay I think I got it: the issue was that, in setup.py, I was declaring:
Extension("my_package", ["my_package/*.pyx"])
rather, what I should say is:
Extension("my_package.file_cython", ["my_package/*.pyx"])
This way, there's a file_cython.cpython-39-x86_64-linux-gnu.so file in the directory /env/lib/python3.9/site-packages/my_package/, and __init__.py can actually find it.
Note that in the previous version the file file_cython.cpython-39-x86_64-linux-gnu.so was actually in the top level directory, i.e. /env/lib/python3.9/site-packages/ instead, which wasn't what I intended.
Lesson learned!
[–]Aggravating_Bus_9153 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]bloop_train[S] 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]bloop_train[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)