[Distutils] c extensions: how to rebuild and test quickly

Thomas Heller theller at ctypes.org
Thu Feb 15 07:58:03 CET 2007

Paul Pogonyshev schrieb:
> Hi,
> Distutils create dynamic library for a C extension in `build/...'
> directory.  This makes it impossible to run program without
> installing it, which I find very important for developing.  Ideally,
> process should look like this:
>     edit Python code -> test (uninstalled);
> or
>     edit C code -> ./setup.by build -> test (uninstalled).
> This is all possible if I create a symbolic link from build
> directory to the sources.  Then extension module can be imported
> normally and everything is like it was with all code limited to
> Python.
> Is it possible in some standard (and preferably portable) way with
> distutils?  Is it already done?

What I do is to create a loader module named <extension>.py that 
'replaces' itself with the real extension, loading it via imp
from the platform specific build directory that distutils creates.

Here is the code:

def _boot():
    import sys, os, imp
    from distutils.util import get_platform

    plat_specifier = ".%s-%s" % (get_platform(), sys.version[0:3])
    build_dir = os.path.join(os.path.dirname(__file__), 'build', 'lib' + plat_specifier)

    file, path, descr = imp.find_module(__name__, [build_dir])
    imp.load_module(__name__, file, path, descr)

_boot(); del _boot


More information about the Distutils-SIG mailing list