1

Let's say I have a directory "pylib" with this in it:

pylab/funcs.py:

def add(a, b): return a + b 

pylab/__init__.py:

from .funcs import add 

It is then possible to do the following:

$ python -c "import pylib as lib; print(lib.add(5, 6))" 11 

How can I achieve the same effect with Cython with a similar organizational structure? That is, if I just have a single file, like:

cylab.pyx:

def add(a, b): return a + b 

I can create a setup script

setup.py:

from distutils.core import setup from distutils.extension import Extension from Cython.Build import cythonize extensions = [ # I will eventually be referencing C libraries Extension('cylib', ['cylib.pyx'], libraries=[], library_dirs=[]) ] setup( ext_modules = cythonize(extensions) ) 

and then the rest works in a fairly straightforward way

$ ./setup.py build_ext --inplace [omitted] $ python -c "import cylib as lib; print(lib.add(5, 6))" 11 

but it is not clear to me how I'm supposed to break it up into separate files, nor how I'm supposed to modify the setup script once I do.

1 Answer 1

1

It's not really possible to build a single Cython extension containing submodules. I think your best option is to build lots of individual Cython libraries.

Just rename all your .py to .pyx to generate a bunch of Cython extensions. The structure will be the same as it was before. I'd exclude the module __init__.py and keep that as pure Python (the rules for how Python finds modules in this case are a little more complicated.

You want to list all of your .pyx files as separate extensions in setup.py:

extensions = [ # I will eventually be referencing C libraries Extension('cylib.funcs', ['cylib/funcs.pyx'], libraries=[], library_dirs=[]), Extension('cylib.something_else', ['cylib/something_else.pyx'], libraries=[], library_dirs=[]) ] 

(and probably also tell setup.py that the whole cylib directory is a package, as you would when using distutils/setuptools with similar pure Python modules.)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.