I have written 3 Cython extensions, and I would like to package them in a tidy way that I can distribute to coworkers to use in Python, without them needing a C compiler installed.
Directory structure is:
myproject\
|---- setup.py
|---- gamma-source\
| |---- eta.pxd
| |---- eta.pyx
| |---- omega.pxd
| |---- omega.pyx
| |---- rho.pxd
| |---- rho.pyx
Where omega
has a cimport from rho
, eta
has a cimport and an import from omega
, rho
has cimports from eta
and omega
.
My setup.py
is:
from setuptools import setup, Extension
from Cython.Build import cythonize
__version__ = '0.1.0'
exts = [Extension("eta",
sources = ["gamma-source\\eta.pyx"]),
Extension("omega",
sources = ["gamma-source\\omega.pyx"]),
Extension("rho",
sources = ["gamma-source\\rho.pyx"]),
]
setup(name="gamma",
version=__version__,
ext_modules = cythonize(exts,
compiler_directives = {'language_level':'3'}
),
zip_safe = False,
)
and using python setup.py install
gets me working on my own machine, but:
- The
.pyd
files are dropped into mysite-packages
folder rather than being insite-packages\rho
, which is untidy. - This method requires me to
import eta, omega, rho
instead offrom gamma import omega
orimport gamma.omega
, which I would prefer if possible.
I tried changing setup.py to:
exts = [Extension("gamma.eta",
sources = ["gamma-source\\eta.pyx"]),
Extension("gamma.omega",
sources = ["gamma-source\\omega.pyx"]),
Extension("gamma.rho",
sources = ["gamma-source\\rho.pyx"]),
]
but python setup.py develop
gives me:
error: could not create 'gamma\rho.cp39-win_amd64.pyd': No such file or directory.
I then tried putting a gamma
directory into myproject
, and then it would install, but I found that
from gamma import omega
worked but importing either eta
and rho
gave me
ModuleNotFoundError: No module named 'rho'
I then tried changing the imports from from omega cimport omega_class
to from .omega cimport omega_class
in the hope that that would get the modules to find each other, but that wouldn't compile - eta.pyx
couldn't find omega_class
.
I tried adding a blank __init__.py
file to gamma-source
, but that also wouldn't compile.
Where am I going wrong? How should I package these extensions into a coherent unit? I'm happy to share more detailed error info from any of the approaches I took, but not sure which of them would be fruitful.
This question looked like it might be hitting the same issue, but it wasn't clear to me how I would apply the given solution: Building Python package containing multiple Cython extensions