我试图使用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版本,并且使用的不是系统的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>
其他回答
对于OpenSuse的同志们:
sudo zypper install python3-devel
在AWS API(centOS)中
yum install python27-devel
有时,即使在安装python dev之后,错误仍然存在,如果缺少“gcc”,请检查错误。
第一次下载,如中所述https://stackoverflow.com/a/21530768/8687063,然后安装gcc
对于apt(Ubuntu、Debian…):
sudo apt-get install gcc
对于yum(CentOS,RHEL…):
sudo yum install gcc
对于dnf(Fedora…):
sudo dnf install gcc
对于zypper(openSUSE…):
sudo zypper in gcc
对于apk(Alpine…):
sudo apk gcc
这里还有另一个解决方案,因为这些解决方案都不适合我。作为参考,我试图在AmazonLinuxAMI基础Docker镜像上安装Python 3.6的一些东西。
非码头解决方案:
# Install python3-devel like everyone says
yum -y install python36-devel.x86_64
# Find the install directory of `Python.h`
rpm -ql python36-devel.x86_64 | grep -i "Python.h"
# Forcefully add it to your include path
C_INCLUDE_PATH='/usr/include/python3.6m'
export C_INCLUDE_PATH
Docker解决方案:
# Install python3-devel like everyone says
RUN yum -y install python36-devel.x86_64
# Find the install directory of `Python.h`, for me it was /usr/include/python3.6m
RUN rpm -ql python36-devel.x86_64 | grep -i "Python.h" && fake_command_so_docker_fails_and_shows_us_the_output
# Since the previous command contains a purposeful error, remove it before the next run
# Forcefully add it to your include path
ARG C_INCLUDE_PATH='/usr/include/python3.6m'
注意:如果在编译C++时遇到错误,请使用CPLUS_INCLUDE_PATH。
或者,您可能更喜欢使用另一个Docker映像。例如,我试图在python:3.9.4-slim上安装asyncpg~=0.24.0,这产生了与您看到的相同的错误。然而,当我将图像更新为python:3时,它工作得很好。
如果您已经安装了Python,并且希望链接到特定的Python版本,则可以从Python获取相关的include路径。
>>> import sysconfig
>>> sysconfig.get_path('include')
'/usr/include/python3.10' # Example output
您可以使用shell脚本手动指定特定python版本的包含路径/构建:
#!/bin/sh
python_include=$(python3.10 -c "import sysconfig; print(sysconfig.get_path('include'))")
gcc -Wall utilsmodule.c -o Utilc -I"$python_include"