我正在使用pybind11为我的 C++ 代码创建 Python 绑定(bind)的库。
当我编译包含 <pybind11/pybind11.h>
的绑定(bind)代码文件时,它会生成以下错误:
/usr/local/include/pybind11/detail/common.h:112:10: fatal error: 'Python.h' file
not found
#include <Python.h>
我可以通过将其更改为 #include <Python/Python.h>
来修复此错误但该库使用 Python 2.7 来生成绑定(bind)。
所以我尝试将其更改为 #include "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/include/python3.7m/Python.h"
现在该库使用 Python 3.7 生成绑定(bind),这正是我想要的。
虽然这种方法有效,但我想知道是否有任何更简洁的方法可以使库始终包含来自 Python3 而不是 Python2 的 header 。
提前谢谢您!
PS:我使用的是 macOS 10.15.2
最佳答案
有几种方法,但据我所知,没有一种方法在所有平台上都是一致的(这就是为什么像 cmake (参见: https://github.com/pybind/cmake_example )这样的方法通常是首选的原因)。
首先是python-config
,即添加:
`python-config --includes`
(带反引号)到 CLI。我的问题是,它是通过 $PATH
找到的(因此,如果该安装没有 python
,则不需要匹配您正在运行的 python-config
版本)并且根据发行版,可能会有 python-config
。和python3-config
对于 python2
和python3
分别。
其次,有模块 distutils:
`python3 -c 'import distutils.sysconfig as ds; print(ds.get_python_inc())'`
它的优点是从实际的 python
运行你选择的。一般来说,distutils 跨平台也不完全一致,但是 get_python_inc
是一个安全的选择。
关于python - 如何强制库(pybind11)包含Python3中的<Python.h>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59572655/