Linux无sudo权限下pip安装Pillow失败:gcc编译报错(Python.h缺失)
无sudo权限下Linux安装Pillow 8.4(Python3.6)遇Python.h缺失问题
问题背景
在Linux虚拟机中尝试安装Pillow 8.4(对应Python3.6),但没有sudo权限,无法通过系统包管理器安装Python开发依赖。执行以下两条命令均失败:
python3 -m pip install Pillow
python3 -m pip install Pillow --upgrade
核心错误
编译过程中提示头文件缺失:
fatal error: Python.h: No such file or directory
最终终止报错:
error: command 'gcc' failed with exit status 1
完整错误输出(省略20+条成功编译日志):
Collecting Pillow Using cached https://files.pythonhosted.org/packages/7d/2a/2fc11b54e2742db06297f7fa7f420a0e3069fdcf0e4b57dfec33f0b08622/Pillow-8.4.0.tar.gz Installing collected packages: Pillow Running setup.py install for Pillow ... error Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-go2j0sv9/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-piu2gd0c-record/install-record.txt --single-version-externally-managed --compile: running install running build running build_py creating build creating build/lib.linux-x86_64-3.6 creating build/lib.linux-x86_64-3.6/PIL copying src/PIL/IcnsImagePlugin.py -> build/lib.linux-x86_64-3.6/PIL # x20 copying lines removed running egg_info writing src/Pillow.egg-info/PKG-INFO writing dependency_links to src/Pillow.egg-info/dependency_links.txt writing top-level names to src/Pillow.egg-info/top_level.txt reading manifest file 'src/Pillow.egg-info/SOURCES.txt' reading manifest template 'MANIFEST.in' warning: no files found matching '*.c' warning: no files found matching '*.h' warning: no files found matching '*.sh' warning: no previously-included files found matching '.appveyor.yml' warning: no previously-included files found matching '.clang-format' warning: no previously-included files found matching '.coveragerc' warning: no previously-included files found matching '.editorconfig' warning: no previously-included files found matching '.readthedocs.yml' warning: no previously-included files found matching 'codecov.yml' warning: no previously-included files matching '.git*' found anywhere in distribution warning: no previously-included files matching '*.pyc' found anywhere in distribution warning: no previously-included files matching '*.so' found anywhere in distribution no previously-included directories found matching '.ci' writing manifest file 'src/Pillow.egg-info/SOURCES.txt' running build_ext building 'PIL._imaging' extension building 'PIL._imagingft' extension creating build/temp.linux-x86_64-3.6 creating build/temp.linux-x86_64-3.6 building 'PIL._imagingtk' extension creating build/temp.linux-x86_64-3.6/src creating build/temp.linux-x86_64-3.6/src creating build/temp.linux-x86_64-3.6/src/Tk gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -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-go2j0sv9/Pillow -I/usr/include/fribidi -I/usr/include -I/usr/local/include -I/usr/include/python3.6m -c src/_imagingtk.c -o build/temp.linux-x86_64-3.6/src/_imagingtk.o creating build/temp.linux-x86_64-3.6/src/libImaging gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -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-go2j0sv9/Pillow -I/usr/include/fribidi -I/usr/include -I/usr/local/include -I/usr/include/python3.6m -c src/_imagingft.c -o build/temp.linux-x86_64-3.6/src/_imagingft.o building 'PIL._imagingmath' extension gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -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-go2j0sv9/Pillow -I/usr/include/fribidi -I/usr/include -I/usr/local/include -I/usr/include/python3.6m -c src/_imagingmath.c -o build/temp.linux-x86_64-3.6/src/_imagingmath.o gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -DHAVE_LIBJPEG -DHAVE_LIBZ -DHAVE_LIBTIFF -DHAVE_XCB -DPILLOW_VERSION="8.4.0" -I/usr/include/freetype2 -I/tmp/pip-build-go2j0sv9/Pillow -I/usr/include/fribidi -I/usr/include -I/usr/local/include -I/usr/include/python3.6m -c src/_imaging.c -o build/temp.linux-x86_64-3.6/src/_imaging.o src/_imagingft.c:22:20: fatal error: Python.h: No such file or directory #include "Python.h" ^ compilation terminated. src/_imagingtk.c:15:20: fatal error: Python.h: No such file or directory #include "Python.h" ^ compilation terminated. src/_imagingmath.c:16:20: fatal error: Python.h: No such file or directory #include "Python.h" ^ compilation terminated. src/_imaging.c:75:20: fatal error: Python.h: No such file or directory #include "Python.h" ^ compilation terminated. building 'PIL._imagingmorph' extension gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -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-go2j0sv9/Pillow -I/usr/include/fribidi -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/_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
解决方案
1. 直接安装预编译wheel包
跳过本地编译,用pip下载预编译好的二进制包:
python3 -m pip install Pillow==8.4.0 --only-binary :all:
如果缓存导致问题,添加--no-cache-dir参数重新安装:
python3 -m pip install Pillow==8.4.0 --only-binary :all: --no-cache-dir
2. 指定Python头文件路径(若能找到)
如果系统中存在Python3.6的头文件但不在默认搜索路径,先定位头文件位置:
python3 -c "import sysconfig; print(sysconfig.get_path('include'))"
假设输出是/opt/python3.6/include/python3.6m,设置环境变量后安装:
export CFLAGS="-I/opt/python3.6/include/python3.6m" python3 -m pip install Pillow==8.4.0
3. 用pyenv搭建独立Python环境
如果允许在用户目录下安装工具,可以用pyenv自己编译带完整头文件的Python3.6:
- 克隆pyenv到用户目录:
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
- 配置shell环境变量(根据你用的shell修改
~/.bashrc或~/.zshrc):
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bashrc
- 重启shell或加载配置:
source ~/.bashrc
- 安装Python3.6.15(最新的3.6分支稳定版):
pyenv install 3.6.15
- 设置当前shell使用这个Python版本:
pyenv local 3.6.15
- 安装Pillow:
pip install Pillow==8.4.0
内容的提问来源于stack exchange,提问作者cultured_left_foot
相关产品推荐
相关产品推荐

