Pybind11: CMake project build fails, cannot find Python.h

Created on 13 Mar 2019  路  13Comments  路  Source: pybind/pybind11

Not sure if this is a PyBind11 problem per se, but it's one that I've encountered twice on this project, in the context of PyBind11.

I think I've solved it, but I couldn't find any other posts about the issue, so let this stand as possibly useful for future users.

My stack is a bit opaque, so I can't really explain what might have caused the problem.

For a project I'm working on, which builds using PyBind11 as an option, I encountered the following problem:

pybind11/include/pybind11/detail/common.h:112:10: fatal error: Python.h: No such file or directory
 #include <Python.h>
          ^~~~~~~~~~          

I fixed it then, but I'm not sure how. Later, when moving the project to another machine, I encountered the issue again.

After a while of trying to pinpoint it, I found that ensuring the python3-dev apt package was installed got rid of the issue. It's possible that switching pybind11 branch helped too.

Most helpful comment

I'm on macOS 10.15, so I can give some hints.

  1. you must have proper Python installation somewhere on your machine including development headers/libraries
  2. your compiler must have proper flags to search Python include directory
    -I/some/path/to/python/include/dir
  3. your linker must have proper flags to link against proper Python library
  4. (optiona) your IDE should be aware of this

On macOS I would recommend installing latest Python3 via homebrew.
No matter how you install your Python or whether you use the system Python, you should have it on path and be able to consult python3-config to give you some answers to above questions.

Specifically:

  1. python3-config --prefix
  2. python3-config --include
  3. python3-config -ldflags

All 13 comments

similar issue.

Same issue here when I tried the example, and I want to use pybind11 with python2.7.

g++ -O3 -Wall -shared -std=c++11 -fPIC python -m pybind11 --includes example.cpp -o examplepython-config --extension-suffix

It can work with python3, I wonder how should I use it with python2.7 ?

Thanks

I suspect on my side of things it might be a virtual environment issue, but I can never reproduce it, or fix it in a consistent way.

@i-chaochen, I've been using CMake, and setting the Python version using the flag -DPYBIND11_PYTHON_VERSION=3.5.

I haven't built systems with PyBind11 using standard build commands, so perhaps someone else can illuminate. What would happen if you replace the call to python in the command with python2.7?

I suspect on my side of things it might be a virtual environment issue, but I can never reproduce it, or fix it in a consistent way.

@i-chaochen, I've been using CMake, and setting the Python version using the flag -DPYBIND11_PYTHON_VERSION=3.5.

I haven't built systems with PyBind11 using standard build commands, so perhaps someone else can illuminate. What would happen if you replace the call to python in the command with python2.7?

Thanks, I just tried it and it's the same, my default python is python2.7.15rc1.

same problem, any fix available?

centos7 yum install python-devel

what about windows?

Haven't tried on Windows, what is your setup like?

My heuristic when this has come up again for me has been:

  1. make sure python dev packages are installed, for the version of python I'm targeting
  2. ensure I'm not compiling inside a python virtual environment as this has inconsistently caused issues

Solved on my CentOS 7.7 via yum install python36-devel. Check if there's a similar python or python3 dev or devel package you can install to resolve.

I wander how to solve this problem under macOS :D

I'm on macOS 10.15, so I can give some hints.

  1. you must have proper Python installation somewhere on your machine including development headers/libraries
  2. your compiler must have proper flags to search Python include directory
    -I/some/path/to/python/include/dir
  3. your linker must have proper flags to link against proper Python library
  4. (optiona) your IDE should be aware of this

On macOS I would recommend installing latest Python3 via homebrew.
No matter how you install your Python or whether you use the system Python, you should have it on path and be able to consult python3-config to give you some answers to above questions.

Specifically:

  1. python3-config --prefix
  2. python3-config --include
  3. python3-config -ldflags

I am trying to use pybind11 on a cluster. I have loaded module python/3.6-anaconda-5.2 and I am encountering this error. Could someone please help me with a solution for this?

Thanks, @darwin & @edrozenberg & @wangdxh for answering!

See also the answer in #1781.

Was this page helpful?
0 / 5 - 0 ratings