Malvineous Malvineous - 2 months ago 15
Python Question

How to use autotools to build Python interface at same time as library

I currently have a library written in C++, building with the GNU autotools, and I'd like to add a Python interface to it. Using SWIG I have developed the interface, but I'm having some trouble figuring out how integrate compilation of the Python module in with the rest of the process.

I have looked into AM_PATH_PYTHON but this macro doesn't seem to set the include path for Python.h, so when I compile my module I get a bunch of errors about missing include files. Is there a way to get the Python include path and ldflags out of AM_PATH_PYTHON?

Just for the record I don't think it will be possible to use Python's distutils method ( as this requires the location of the library in order to link the new module. Since the library has not yet been installed at compile time, I would have to use a relative path (e.g. ../src/ which of course would break once the Python module was installed (as the library is then in /usr/lib or /usr/local/lib instead.)


Now it can find the .h file it's compiling, but after installing it (in the correct location) Python can't load the module. The code produces, and when I "import foo" I get this:

ImportError: dynamic module does not define init function (initfoo)

If however I rename it from to then it loads and runs fine, except I have to "import _foo" which I'd rather not have to do. When I follow the SWIG instructions to produce in the current directory "import foo" works, so I am not sure why it breaks when the library is installed in the site directory.


Turns out the problem was I forgot to copy produced by SWIG into the install directory alongside Once I did this everything worked as expected! Now I just have to figure out some automake rules to copy a file into the install dir...


To find the include path, I'd use python-config. The trick is to use the python-config corresponding to the python installed in $PYTHON.

AC_ARG_VAR([PYTHON_INCLUDE], [Include flags for python, bypassing python-config])
AC_ARG_VAR([PYTHON_CONFIG], [Path to python-config])
AS_IF([test -z "$PYTHON_INCLUDE"], [
  AS_IF([test -z "$PYTHON_CONFIG"], [
                  [python$PYTHON_VERSION-config python-config],
                  [`dirname $PYTHON`])
    AS_IF([test "$PYTHON_CONFIG" = no], [AC_MSG_ERROR([cannot find python-config for $PYTHON.])])
  AC_MSG_CHECKING([python include flags])

Another alternative is to poke around in the distutils.sysconfig module (this has nothing to do with using distutils to build your code). Run python -c "import distutils.sysconfig; help(distutils.sysconfig)" and have a look.