Search code examples
cythondistutils

How to use a cython definition file (pxd) without making it an extension ?


What is the simplest way to create a pxd definition file, which simply collects cdefs replicated over pyx files, without creating new extensions?

My case is the following: I would like to gather some extern cdefs in a pxd file (hp/src/common.pxd). I've also added some non-extern cdef, whose implementation is in common.pyx.

In another pyx file (hp/src/_lib.pyx), which I turn into an extension, I cimport some stuff from common.

In the setup.py file I create the following extension:

Extension('hp._lib', 
          ['hp/src/_lib.pyx'],
          language='c++'),

By doing so, no common.cpp file is created, so it looks like dependencies are not automatically handled. That's the first problem.

Then, manually running 'cython --cplus common.pyx' correctly creates a common.cpp file in the directory hp/src, and if I add 'hp/src/common.cpp' to the list of the extension sources, the command python setup.py installs everything without complaint, but then, importing the module hp triggers an ImportError: No module named common... from _lib.cpp... I'm stuck here. Any idea?


Solution

  • You can include material from another file using the include statement.

    See the documentation here:

    http://docs.cython.org/src/userguide/language_basics.html#the-include-statement

    If you have external definitions in common.pxd and implementations in commond.pyx you will need to include them both. For example, in _lib.pyx:

    include "common.pxd"
    include "common.pyx"
    

    The contents of these files will then be compiled into your _lib module as if they were textually included at that point. i.e. a common.c file will not be generated, but rather included with _lib.c.

    The Cython compiler doesn't recognise changes to includes when deciding if the .c file for the module needs to be regenerated, so remember to delete this before you recompile.