我试图使用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没有问题。我设法在我的机器上找到了该文件。
当前回答
如果您在Amazon Linux上使用Python 3.6(基于RHEL,但此处给出的RHEL答案无效):
sudo yum install python36-devel
其他回答
当您尝试删除python3.5并安装python3.6时,通常会出现这种情况。
因此,当使用python3(即python3-V=>python3.6)安装某些所需的包时,python3.5标头将显示此错误。
通过安装python3.6-dev模块解决。
看起来您没有正确安装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。
当然,python-dev或libpython-all-dev是(apt)安装的第一件事,但如果这对我的情况没有帮助,我建议您通过sudo apt-get-install libffi-dev和sudo pip-install cffi安装外部函数接口包。
这将有助于解决问题,尤其是当您看到错误为/from c/_cffi_backend.c:2:20:致命错误:Python.h:没有这样的文件或目录时。
当您安装了不同的Python版本,并且使用的不是系统的pip时,也会出现此问题。在这种情况下,非系统pip无法找到正确版本的Python头。
我在尝试pip安装与应用程序捆绑的Python包时遇到了这种情况。由于不是系统的python,apt-installpythonXX-dev无法工作。
在这种情况下,解决方案是找到正确的python头:
find / -iname 'Python.h'
在输出中,您将看到系统python头文件,希望是您要查找的头文件,例如:
/usr/include/python3.7m/Python.h
/usr/include/python3.6m/Python.h
/home/ubuntu/workspace/blender-git/lib/linux_centos7_x86_64/python/include/python3.7m/Python.h
/home/ubuntu/miniconda3/pkgs/python-3.8.5-h7579374_1/include/python3.8/Python.h
/home/ubuntu/miniconda3/pkgs/python-3.7.0-h6e4f718_3/include/python3.7m/Python.h
/home/ubuntu/miniconda3/include/python3.8/Python.h
/home/ubuntu/miniconda3/envs/sim/include/python3.7m/Python.h
/home/ubuntu/src/blender-deps/Python-3.7.7/Include/Python.h
/opt/lib/python-3.7.7/include/python3.7m/Python.h
然后,您可以设置一个编译器标志,当被pip调用时,它将被gcc使用。我的是/home/ubuntu/workspace/binder git/lib/linux_centos7_x86_64/python/include/python3.7m/python.h,所以我这样做了:
export CPPFLAGS=-I/home/ubuntu/src/blender-deps/Python-3.7.7/Include
pip install <package>
你必须做两件事。
安装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