Using Sphinx with a distutils-built C extension

Posted by detly on Stack Overflow See other posts from Stack Overflow or by detly
Published on 2011-01-14T03:05:43Z Indexed on 2011/01/17 2:53 UTC
Read the original article Hit count: 280

Filed under:
|
|

I have written a Python module including a submodule written in C: the module itself is called foo and the C part is foo._bar. The structure looks like:

src/ 
  foo/__init__.py   <- contains the public stuff 
  foo/_bar/bar.c    <- the C extension
doc/                <- Sphinx configuration
  conf.py
  ...

foo/__init__.py imports _bar to augment it, and the useful stuff is exposed in the foo module. This works fine when it's built, but obviously won't work in uncompiled form, since _bar doesn't exist until it's built.

I'd like to use Sphinx to document the project, and use the autodoc extension on the foo module. This means I need to build the project before I can build the documentation.

Since I build with distutils, the built module ends up in some variably named dir build/lib.linux-ARCH-PYVERSION — which means I can't just hard-code the directory into a Sphinx' conf.py.

So how do I configure my distutils setup.py script to run the Sphinx builder over the built module?

For completeness, here's the call to setup (the 'fake' things are custom builders that subclass build and build_ext):

setup(cmdclass = {
        'fake': fake,
        'build_ext_fake' : build_ext_fake
      },
      package_dir = {'': 'src'},
      packages = ['foo'],
      name = 'foo',
      version = '0.1',
      description = desc,
      ext_modules = [module_real])

© Stack Overflow or respective owner

Related posts about python

Related posts about sphinx