Ubuntu - Linking boost.python - Fatal error: pyconfig cannot be found

Having some issues, now I have read the following:

hello world python extension in c++ using boost?

I have tried installing boost onto my desktop, and, done as the posts suggested in terms of linking. I have the following code:

#include <boost/python.hpp>
#include <Python.h>
using namespace boost::python;

Now I have tried linking with the following:

g++ testing.cpp -I /usr/include/python2.7/pyconfig.h -L /usr/include/python2.7/Python.h
-lpython2.7

And I have tried the following as well:

g++ testing.cpp -I /home/username/python/include/ -L /usr/include/python2.7/Python.h -lpython2.7

I keep getting the following error:

/usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: No such   
file or directory
# include <pyconfig.h>

I don't know where I am going wrong. I do have boost.python installed, there's just a problem linking?


Solution 1:

I just had the same error, the problem is g++ can't find pyconfig.h(shocking, I know). For me this file is located in /usr/include/python2.7/pyconfig.h so appending -I /usr/include/python2.7/ should fix it, alternatively you can add the directory to your path with:

export CPLUS_INCLUDE_PATH="$CPLUS_INCLUDE_PATH:/usr/include/python2.7/"

You can also add this to your .bashrc and it will be added whenever you start your shell next(you will have to reopen your terminal to realize the changes).

You can find your own python include path by using find /usr/include -name pyconfig.h, in my case this returns:

/usr/include/python2.7/pyconfig.h
/usr/include/i386-linux-gnu/python2.7/pyconfig.h

Solution 2:

There two possible causes for this symptom: 1. you don't have python-dev installed. 2. you have python-dev installed and your include path is incorrectly configured, which the above posting provide a solution. In my case, I was installing boost, and it is looking for the pyconfig.h header file that is missing in my ubuntu:

The solution is

apt-get install python-dev

In other linux flavors, you have to figure out how to install python header.

Solution 3:

If you have a .c file (hello.c) and you want to build an libhello.so library, try:

find /usr/include -name pyconfig.h

[out]:

/usr/include/python2.7/pyconfig.h
/usr/include/x86_64-linux-gnu/python2.7/pyconfig.h

then use the output and do:

gcc -shared -o libhello.so -fPIC hello.c -I /usr/include/python2.7/

If you're converting from cython's .pyx to .so, try this python module, it will automatically build the .so file given the .pyx file:

def pythonizing_cython(pyxfile):
    import os
    # Creates ssetup_pyx.py file.
    setup_py = "\n".join(["from distutils.core import setup",
                          "from Cython.Build import cythonize",
                          "setup(ext_modules = cythonize('"+\
                          pyxfile+".pyx'))"])   

    with open('setup_pyx.py', 'w') as fout:
        fout.write(setup_py)

    # Compiles the .c file from .pyx file.
    os.system('python setup_pyx.py build_ext --inplace')

    # Finds the pyconfig.h file.
    pyconfig = os.popen('find /usr/include -name pyconfig.h'\
                        ).readline().rpartition('/')[0]

    # Builds the .so file.
    cmd = " ".join(["gcc -shared -o", pyxfile+".so",
                    "-fPIC", pyxfile+".c",
                    "-I", pyconfig])
    os.system(cmd)

    # Removing temporary .c and setup_pyx.py files.
    os.remove('setup_pyx.py')
    os.remove(pyxfile+'.c')

Solution 4:

I had a similar experience when building boost for centos7. I was not able to find pyconfig.h on my system only pyconfig-64.h.

After searching around I found that you need to install python-devel to get pyconfig.h