我试图使用C扩展名文件构建共享库,但首先必须使用以下命令生成输出文件:

gcc -Wall utilsmodule.c -o Utilc

执行命令后,我收到以下错误消息:

> utilsmodule.c:1:20: fatal error: Python.h: No such file or directory
compilation terminated.

我已经在互联网上尝试了所有建议的解决方案,但问题仍然存在。我对Python.h没有问题。我设法在我的机器上找到了该文件。


当前回答

在Fedora上运行Python 2:

sudo dnf install python2-devel

对于Python 3:

sudo dnf install python3-devel

其他回答

Cygwin解决方案

您需要安装python2-devel或python3-devel包,具体取决于您使用的Python版本。

您可以使用Cygwin.com上的32位或64位setup.exe(取决于您的安装)快速安装它。

示例(如果需要,请修改setup.exe的文件名和Python的主要版本):

$ setup.exe -q --packages=python3-devel

您还可以查看我的另一个答案,以获得从命令行安装Cygwin软件包的更多选项。

特别是对于Python 3.7和Ubuntu,我需要

sudo apt安装libpython3.7-dev

.我想在某些时候,名字从pythom.n-dev改成了这个。

对于Python 3.6、3.8到3.10(以及计数…),类似地:

sudo apt安装libpython3.6-dev 

sudo apt安装libpython3.8-dev 

sudo apt安装libpython3.9-dev

sudo apt安装libpython3.10-dev

尝试定位Python.h:

gemfield@ThinkPad-X1C:~$ locate Python.h
/home/gemfield/anaconda3/include/python3.7m/Python.h
/home/gemfield/anaconda3/pkgs/python-3.7.6-h0371630_2/include/python3.7m/Python.h
/usr/include/python3.8/Python.h

如果找不到,则安装python-dev或python3dev;否则包括编译器的正确标头路径:

g++ -I/usr/include/python3.8 ...

经确认,如果您正在运行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

你必须做两件事。

安装Python开发包,如果是Debian/Uubuntu/Mint,可以使用以下命令完成:

sudo apt-get install python-dev

第二点是,默认情况下,include文件不在include路径中,Python库默认情况下也不与可执行文件链接。您需要添加这些标志(相应地替换Python的版本):

-I/usr/include/python2.7 -lpython2.7 

换句话说,编译命令应该是:

gcc -Wall -I/usr/include/python2.7 -lpython2.7  utilsmodule.c -o Utilc