1

I'm using pybind11 library to create Python bindings for my C++ code.

When I compile my binding code file which includes <pybind11/pybind11.h>, it generates the following error:

/usr/local/include/pybind11/detail/common.h:112:10: fatal error: 'Python.h' file
      not found
#include <Python.h>

I could fix this error by changing it to #include <Python/Python.h> but the library uses Python 2.7 to generate bindings.

So I tried changing it to #include "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/include/python3.7m/Python.h" and now the library uses Python 3.7 to generate bindings which is what I want.

Although this method works, I wonder if there's any cleaner way to make the library always include headers from Python3 instead of Python2.

Thank you in advance!

P.S: I'm using macOS 10.15.2

1 Answer 1

3

There are a couple of ways, but AFAIK, none are consistent across all platforms (which is why something like cmake (see: https://github.com/pybind/cmake_example) is often preferred).

First, there is python-config, i.e. add:

`python-config --includes`

(with back-ticks) to the CLI. My problem with it, is that it is found through $PATH (thus need not match the version of python that you are running if that installation did not have a python-config) and depending on the distribution, there may be both python-config and python3-config for python2 and python3 respectively.

Second, there is module distutils:

`python3 -c 'import distutils.sysconfig as ds; print(ds.get_python_inc())'`

which has the advantage of being run from the actual python that you choose. In general, distutils isn't fully consistent across platforms either, but get_python_inc is a safe bet.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.