centos系统,执行pip命令报错:

exceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/usr/include/freetype2 -I/tmp/pip-build-t64y0m1t/pillow -I/usr/include -I/usr/local/include -I/usr/include/python3.6m -c src/_imagingmorph.c -o build/temp.linux-x86_64-3.6/src/_imagingmorph.o
    src/_imagingmath.c:16:20: fatal error: Python.h: No such file or directory
     #include "Python.h"
                        ^
    compilation terminated.
    src/_imagingmorph.c:14:20: fatal error: Python.h: No such file or directory
     #include "Python.h"
                        ^
    compilation terminated.
    error: command 'gcc' failed with exit status 1
    
    ----------------------------------------
Command "/usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-t64y0m1t/pillow/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-gvrsv709-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-t64y0m1t/pillow/

解决办法:


1. 安装Python开发包

使用yum来安装python3-devel包:


sudo yum install python3-devel

2. 安装其他依赖库

在安装Pillow等库时,还需要一些其他依赖库。以下是常见依赖库的安装命令:

sudo yum install gcc
sudo yum install libjpeg-devel
sudo yum install zlib-devel
sudo yum install freetype-devel
sudo yum install lcms2-devel
sudo yum install libwebp-devel
sudo yum install tcl-devel tk-devel
sudo yum install harfbuzz-devel
sudo yum install fribidi-devel

3. 重新安装Pillow

安装完所有依赖库后,可以重新尝试安装Pillow:

pip install pillow