52

有一些问题,现在我已经阅读了以下内容:

使用boost在c ++中使用hello world python扩展?

我已经尝试将 boost 安装到我的桌面上,并且按照链接方面建议的帖子完成。我有以下代码:

#include <boost/python.hpp>
#include <Python.h>
using namespace boost::python;

现在我尝试与以下链接:

g++ testing.cpp -I /usr/include/python2.7/pyconfig.h -L /usr/include/python2.7/Python.h
-lpython2.7

我也尝试了以下方法:

g++ testing.cpp -I /home/username/python/include/ -L /usr/include/python2.7/Python.h -lpython2.7

我不断收到以下错误:

/usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: No such   
file or directory
# include <pyconfig.h>

我不知道我哪里错了。我确实安装了 boost.python,只是链接有问题?

4

7 回答 7

102

我刚刚遇到了同样的错误,问题是 g++ 找不到 pyconfig.h(震惊,我知道)。对我来说,这个文件位于/usr/include/python2.7/pyconfig.h所以附加-I /usr/include/python2.7/应该修复它,或者你可以将目录添加到你的路径:

export CPLUS_INCLUDE_PATH="$CPLUS_INCLUDE_PATH:/usr/include/python2.7/"

您也可以将其添加到您的 .bashrc 中,并且它将在您下次启动 shell 时添加(您必须重新打开终端才能实现更改)。

您可以使用 找到您自己的 python 包含路径find /usr/include -name pyconfig.h,在我的情况下返回:

/usr/include/python2.7/pyconfig.h
/usr/include/i386-linux-gnu/python2.7/pyconfig.h
于 2014-03-26T23:00:07.837 回答
22

这个症状有两个可能的原因:1.你没有安装python-dev。2. 你安装了 python-dev 并且你的包含路径配置不正确,上面的帖子提供了一个解决方案。就我而言,我正在安装 boost,它正在寻找我的 ubuntu 中缺少的 pyconfig.h 头文件:

解决方案是

apt-get install python-dev

在其他 linux 风格中,您必须弄清楚如何安装 python 头文件。

于 2016-12-03T06:08:43.687 回答
9

如果您有一个.c文件 ( hello.c) 并且想要构建一个libhello.so库,请尝试:

find /usr/include -name pyconfig.h

[出去]:

/usr/include/python2.7/pyconfig.h
/usr/include/x86_64-linux-gnu/python2.7/pyconfig.h

然后使用输出并执行:

gcc -shared -o libhello.so -fPIC hello.c -I /usr/include/python2.7/

如果您要从 cython 的 .pyx 转换为 .so,请尝试使用这个 python 模块,它会根据 .pyx 文件自动构建 .so 文件:

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')
于 2014-05-18T15:39:54.820 回答
8

在为 centos7 构建 boost 时,我也有类似的经历。我无法在我的系统上找到 pyconfig.h,只有 pyconfig-64.h。

找了一圈发现需要安装python-devel才能获取pyconfig.h

于 2014-11-16T15:38:34.310 回答
5

对于 CentOS,请执行以下操作:yum install python-devel. 然后再试一次。

于 2019-01-20T02:26:11.533 回答
2

就我而言,我必须在我的目录/usr/include/中创建一个软链接

ln -s python3.5m python3.5

问题是我使用的是 python 3.5,但只有 python3.5m 目录存在,所以它无法找到pyconfig.h文件。

于 2020-02-26T14:56:59.883 回答
2

如果您有多个 Python 安装,sysconfig 模块可以报告给定安装的 pyconfig.h 的位置。

$ /path/to/python3 -c 'import sysconfig; print(sysconfig.get_config_h_filename())'
/path/to/pyconfig.h    
于 2020-05-27T21:07:46.177 回答