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

7

58

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?

Fascicle answered 6/11, 2013 at 11:30 Comment(2)
First, In all the examples I see and in my code there is no space between the -I and path after (same for -L). Second, Are you sure your path is correct? usually the python include files are in /usr/include/pythonDysphoria
Those are compiler errors, not linker errors. Do you have the Python developer header files installed? They are usually distributed in a developer package, such as python-dev.Everard
K
112

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
Kyat answered 26/3, 2014 at 23:0 Comment(4)
Also, because I cannot comment directly on your question(<50 rep) having a space after the -L is valid, though not common.Kyat
@JaconHacker Your solution works for me. However, I prefer to use -I /usr/include/python2.7 instead. I am familiar with make but where should I add - I /usr/....... to the Jamfile?Peyote
@Peyote Not super familiar with jam, but it looks like it supports a cxxflags argument, so try something like cxxflags=-I /usr/...Kyat
just install python-devel which fixes this problem.Pouch
C
25

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.

Chema answered 3/12, 2016 at 6:8 Comment(2)
or apt-get install python3-dev if you are that way inclined`Educated
python3-dev didn't resolve the proplem in my case. Only python-dev did it.Styria
P
9

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')
Phyllisphylloclade answered 18/5, 2014 at 15:39 Comment(1)
installing python-devel is the proper solution.Pouch
B
8

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

Betweenwhiles answered 16/11, 2014 at 15:38 Comment(1)
fedora24 also needed: dnf install python-develBrutal
C
5

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

Clearing answered 20/1, 2019 at 2:26 Comment(0)
C
2

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.

Celebrant answered 26/2, 2020 at 14:56 Comment(0)
T
2

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    
Tacheometer answered 27/5, 2020 at 21:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.