我一直在尝试使用 pip 安装 Pillow 包(python 版本 3.6,Pillow 版本 8.4,Linux 虚拟机)。其他几个答案建议安装开发人员包,但我没有 sudo 权限。
我收到“致命错误:Python.h:没有这样的文件或目录”和“错误:命令'gcc'失败,退出状态1” “
我都尝试过:
python3 -m pip install Pillow
和
python3 -m pip install Pillow --upgrade
都给出了以下输出/错误(省略了超过 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
错误“致命错误:Python.h:没有这样的文件或目录”表示的系统上缺少 Python 开发标头。这些标头对于构建需要与 Python 解释器交互的 C 扩展(如 Pillow)至关重要。
即使没有 sudo 权限,以下步骤也可以帮助在 Linux 虚拟机上安装 Pillow:
1. 使用虚拟环境:
-
创建虚拟环境:
这会在项目中创建一个独立的环境,避免与系统级包发生冲突。
bash python3 -m venv .venv
-
激活环境:
bash source .venv/bin/activate
-
安装 pip:
某些虚拟环境可能需要单独安装 pip。
bash curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py python3 get-pip.py
2. 安装 Python 开发标头:
由于没有 sudo 权限,因此需要将 Python 开发标头安装到的用户目录中。可以从源代码构建它们,或者,如果可用,请使用的包管理器安装它们,同时指定的用户目录作为安装路径。
-
查找的 Python 版本:
bash python3 --version
假设的版本是 3.6.x,请将“x”替换为的次要版本号。 -
使用的发行版的包管理器(如果可用):
-
Debian/Ubuntu(假设的用户主目录是
/home/user
):bash python3 -m pip install --user --install-option="--prefix=" --install-option="--install-lib=/home/user/python-libs" pillow
-
Fedora/CentOS/RHEL:
bash python3 -m pip install --user --install-option="--prefix=" --install-option="--install-lib=/home/user/python-libs" pillow
将/home/user
替换为的实际用户主目录。
3. 安装 Pillow:
在的虚拟环境中安装 Pillow:
python3 -m pip install --user pillow
这会尝试在的用户目录中安装 Pillow,从而避免了对系统目录的需要。
4. 验证安装:
python3 -c "from PIL import Image; print(Image)"
如果没有遇到任何错误并且打印了 Pillow 版本,则说明已成功安装 Pillow。
标签:python,pip,python-imaging-library From: 74845379