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

C++UbuntuGccBoostBoost Python

C++ Problem Overview


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?

C++ Solutions


Solution 1 - C++

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 - C++

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 - C++

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 - C++

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

Solution 5 - C++

For CentOS do this: yum install python-devel. Then try again.

Solution 6 - C++

In my case, I had to create a soft link in my dir /usr/include/

ln -s python3.5m python3.5

the probleme was that i was using python 3.5 but only the python3.5m directory was existing so it wasn't able to find the pyconfig.h file.

Solution 7 - C++

In case you have multiple Python installations, the sysconfig module can report the location of pyconfig.h for a given installation.

$ /path/to/python3 -c 'import sysconfig; print(sysconfig.get_config_h_filename())'
/path/to/pyconfig.h    

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPhorceView Question on Stackoverflow
Solution 1 - C++Jacob HackerView Answer on Stackoverflow
Solution 2 - C++Kemin ZhouView Answer on Stackoverflow
Solution 3 - C++alvasView Answer on Stackoverflow
Solution 4 - C++BenView Answer on Stackoverflow
Solution 5 - C++Gamma.XView Answer on Stackoverflow
Solution 6 - C++AlexView Answer on Stackoverflow
Solution 7 - C++Mitch ChapmanView Answer on Stackoverflow