57

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?

Community
  • 1
  • 1
Phorce
  • 4,424
  • 13
  • 57
  • 107
  • 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/python – ManicQin Nov 06 '13 at 15:00
  • 2
    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`. – Tanner Sansbury Nov 06 '13 at 16:49

7 Answers7

109

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
Jacob Hacker
  • 1,501
  • 1
  • 13
  • 16
  • Also, because I cannot comment directly on your question(<50 rep) having a space after the -L is valid, though not common. – Jacob Hacker Mar 29 '14 at 06:31
  • @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`? – Admia Jan 22 '17 at 12:59
  • @Admia Not super familiar with jam, but it looks like it supports a `cxxflags ` argument, so try something like `cxxflags=-I /usr/...` – Jacob Hacker Jan 23 '17 at 14:11
  • 3
    just install python-devel which fixes this problem. – kumar Mar 10 '17 at 15:12
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.

Kemin Zhou
  • 6,264
  • 2
  • 48
  • 56
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')
alvas
  • 115,346
  • 109
  • 446
  • 738
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

Ben
  • 836
  • 1
  • 9
  • 19
5

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

Gamma.X
  • 554
  • 4
  • 9
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.

Alex
  • 448
  • 6
  • 11
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    
Mitch Chapman
  • 71
  • 1
  • 2