我试图使用C扩展名文件构建共享库,但首先必须使用以下命令生成输出文件:
gcc -Wall utilsmodule.c -o Utilc
执行命令后,我收到以下错误消息:
> utilsmodule.c:1:20: fatal error: Python.h: No such file or directory
compilation terminated.
我已经在互联网上尝试了所有建议的解决方案,但问题仍然存在。我对Python.h没有问题。我设法在我的机器上找到了该文件。
我试图使用C扩展名文件构建共享库,但首先必须使用以下命令生成输出文件:
gcc -Wall utilsmodule.c -o Utilc
执行命令后,我收到以下错误消息:
> utilsmodule.c:1:20: fatal error: Python.h: No such file or directory
compilation terminated.
我已经在互联网上尝试了所有建议的解决方案,但问题仍然存在。我对Python.h没有问题。我设法在我的机器上找到了该文件。
当前回答
如果操作系统附带的Python未随附,则必须在操作系统上安装Python开发文件。关于这个问题的许多答案显示了在不同系统上实现这一点的多种方式。当您这样做时,问题是告诉编译器它们的位置以及如何针对它们进行编译。Python附带了一个名为Python-config的程序。对于编译,您需要--includes输出,而对于将程序与Python库链接(将Python嵌入到程序中),则需要--ldflags输出。例子:gcc-c mypythonprogram.c$(python3配置--包含)gcc-o程序mypythonprogram.o$(python3配置--ldflags)
python-config程序可以以python版本命名,例如在Debian、Ubuntu上,可以将其命名为python3-config或python3.6-config。
其他回答
看起来您没有正确安装python dev的头文件和静态库。请使用包管理器在系统范围内安装它们。
对于apt(Ubuntu、Debian…):
sudo apt-get install python-dev # for python2.x installs
sudo apt-get install python3-dev # for python3.x installs
对于yum(CentOS,RHEL…):
sudo yum install python-devel # for python2.x installs
sudo yum install python3-devel # for python3.x installs
对于dnf(Fedora…):
sudo dnf install python2-devel # for python2.x installs
sudo dnf install python3-devel # for python3.x installs
对于zypper(openSUSE…):
sudo zypper in python-devel # for python2.x installs
sudo zypper in python3-devel # for python3.x installs
对于apk(Alpine…):
# This is a departure from the normal Alpine naming
# scheme, which uses py2- and py3- prefixes
sudo apk add python2-dev # for python2.x installs
sudo apk add python3-dev # for python3.x installs
对于apt-cyg(Cygwin…):
apt-cyg install python-devel # for python2.x installs
apt-cyg install python3-devel # for python3.x installs
注意:python3dev不会自动覆盖python3的所有次要版本,如果您正在使用例如python3.8,则可能需要安装python3.8-dev。
如果您使用tox在多个版本的Python上运行测试,则可能需要为正在测试的每个版本的Python安装Python开发库。
sudo apt-get install python2.6-dev
sudo apt-get install python2.7-dev
etc.
经确认,如果您正在运行centos 8+。您需要运行以下操作:
sudo yum -y install python36 python38 python39
sudo yum -y install python36-devel.x86_64 python38-devel.x86_64 python39-devel.x86_64
pip3.8 install cpython
pip3.9 install cpython
pip3.6 install cpython
这不是同一种情况,但它也适用于我,现在我可以在Python3.5中使用SWIG:
我试图编译:
gcc -fPIC -c existe.c existe_wrap.c -I /usr/include/python3.5m/
使用Python 2.7可以很好地工作,而不是使用我的3.5版本:
exist_wrap.c:147:21:致命错误:Python.h:不存在el archivo o eldirectorio编译已终止。
在Ubuntu 16.04安装中运行后:
sudo apt-get install python3-dev # for python3.x installs
现在我可以毫无问题地编译Python3.5:
gcc -fPIC -c existe.c existe_wrap.c -I /usr/include/python3.5m/
在我的例子中,在Ubuntu中修复它的方法是安装libpython-all-dev(如果您使用Python3,则安装libpypython3-all-dev)包。