我试图使用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.h位于/usr/include/python3.8和/usr/include/pyton2.7目录中,只需通过类似的方式给出gcc的路径-I/usr/include/pymon3.8即可python的版本替换为您的版本

其他回答

确保Python开发文件随操作系统一起提供。

您不应该硬编码库和包含路径。相反,使用pkg配置,它将为您的特定系统输出正确的选项:

$ pkg-config --cflags --libs python2
-I/usr/include/python2.7 -lpython2.7

您可以将其添加到gcc行:

gcc -Wall utilsmodule.c -o Utilc $(pkg-config --cflags --libs python2) 

Cygwin解决方案

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

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

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

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

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

看起来您没有正确安装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。

如果您在Amazon Linux上使用Python 3.6(基于RHEL,但此处给出的RHEL答案无效):

sudo yum install python36-devel

尝试apt文件。很难记住丢失文件所在的包名。它是通用的,适用于任何包文件。

例如:

root@ubuntu234:~/auto# apt-file search --regexp '/Python.h$'
pypy-dev: /usr/lib/pypy/include/Python.h
python2.7-dbg: /usr/include/python2.7_d/Python.h
python2.7-dev: /usr/include/python2.7/Python.h
python3.2-dbg: /usr/include/python3.2dmu/Python.h
python3.2-dev: /usr/include/python3.2mu/Python.h
root@ubuntu234:~/auto# 

现在你可以做出专家猜测,从中选择哪一个。