【问题标题】:Distutils can't find Python.hDistutils 找不到 Python.h
【发布时间】:2011-02-24 11:26:45
【问题描述】:

我有一个带有扩展部分的 distutils 设置脚本,看起来像这样:

from distutils.core import setup, Extension

my_module = Extension('my_module',
                sources = ['my_file.c', 'my_other_file.c'])

setup (name = 'my_module',
       version = '1.0',
       description = 'My module',
       ext_modules = [my_module])

运行 setup.py build 在我的 Mac 上运行良好。当我移动到 Debian 机器时,它失败了:

error: Python/Python.h: No such file or directory

我安装了python2.6python2.6-dev,文件位于/usr/include/Python2.6

它为问题文件执行的命令:

gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.6 -c my_module.c -o -build/XYZ/my_module.o

所以它是传入头文件的位置。

Mac 与 Linux 环境之间唯一明显的区别是 gcc-4.2 与 gcc-4.4 以及 Python 2.7 与 Python 2.6

想法?

编辑:

在有问题的 C 文件中:

#include <Python/Python.h>
#include <Python/structmember.h>

【问题讨论】:

  • 你能不能试着把这两行改成#include "Python.h"然后重新编译?
  • 我做到了,看起来可以解决问题。知道为什么它可以在 mac 上与“Python/Python.h”一起使用,但在 linux 上却不行吗?
  • 我认为它只是基于安装选项的目录结构不同,linux在/usr/include/python2.6/下有Python.h,但mac可能在/usr/include/Python/Python.h下,但我没有mac,所以很难说我的假设是否正确。
  • 啊,是的,有不同的方法可以获取适用于 Mac 的 Python!我认为它使用的是“/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/Python.h”(/usr/include 中也有版本)。

标签: python gcc distutils python-c-extension


【解决方案1】:

可能在您的模块中,您需要include "Python.h" 而不是"Python/Python.h"

或者您可以尝试导出包含路径,然后尝试使用 gcc 或 g++ 重新编译?

export C_INCLUDE_PATH=/usr/include/python2.6:$C_INCLUDE_PATH
export CPLUS_INCLUDE_PATH=/usr/include/python2.6:$CPLUS_INCLUDE_PATH

【讨论】:

  • 恰恰相反!原始文件有Python/Python.h(见编辑)。我将其更改为 include &lt;Python.h&gt;。看起来这在两个平台上都有效。对此有何评论?
  • +1 完美无瑕! *你能给我指出一种“GCC 配置备忘单”或类似的东西吗?
【解决方案2】:

就我而言,我缺少 python3-dev,sudo apt-get install python3-dev 已修复它。

【讨论】:

    猜你喜欢
    • 2016-09-17
    • 2015-05-13
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-17
    • 2020-01-21
    相关资源
    最近更新 更多