Distutils 找不到 Python.h

7 投票
2 回答
13141 浏览
提问于 2025-04-16 12:27

我有一个用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])

在我的Mac上运行 setup.py build 一切正常。但当我换到一台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的版本,一个是gcc-4.2,另一个是gcc-4.4,还有Python的版本,一个是Python 2.7,另一个是Python 2.6。

有什么想法吗?

编辑:

在相关的C文件中:

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

2 个回答

2

在我的情况下,我缺少了python3-dev这个东西,运行sudo apt-get install python3-dev就解决了这个问题。

6

也许在你的模块中,你需要用 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

撰写回答