答案是必须安装Pillow而非PIL:PIL已停更十多年,PyPI无此包;报错本质是未装Pillow或装到错误环境,正确命令为pip install Pillow(或--user方式),导入时用from PIL import Image。

pip install pillow 报错:根本不是 Pillow 的问题
报错本质是「没装对东西」或「装到了看不见的地方」。PIL 已停更十多年,pip install PIL 必然失败——PyPI 上根本不存在这个包。所有报 ModuleNotFoundError: No module named 'PIL' 或 ERROR: Could not find a version that satisfies the requirement PIL 的情况,都该立刻停手,改用 pip install Pillow(注意大小写,全小写也可,但官方推荐首字母大写)。
Windows 下 PermissionError: [WinError 5] 拒绝访问
典型表现是 pip 进程被系统拦截,卡在复制文件阶段,尤其常见于 Python 安装在 C:\Program Files\ 下的默认路径。这不是权限设置太低,而是 Windows 对该目录有写入保护。
- 不建议右键属性→安全→手动加权限(易漏、易冲突、重启后可能失效)
- 优先用管理员身份运行命令提示符或 PowerShell,再执行
pip install Pillow - 更稳妥的做法:换用用户级安装,加
--user参数,如pip install --user Pillow,它会把包装进当前用户的%APPDATA%\Python\PythonXX\site-packages\目录,完全绕过系统保护 - 若已用普通权限失败,先删掉临时构建目录(如
C:\Users\XXX\AppData\Local\Temp\pip-build-xxx),再重试
Linux/macOS 下 “zlib is required” 或 “libjpeg not found”
这类错误说明 pip 正在尝试从源码编译 Pillow,但系统缺少图像处理底层依赖。Windows 预编译包自带这些库,所以一般不报;而 Linux/macOS 默认不带,必须手动补全。
- Ubuntu/Debian 系:运行
sudo apt-get install libjpeg-dev libpng-dev libtiff-dev libfreetype6-dev liblcms2-dev libwebp-dev zlib1g-dev - CentOS/RHEL/Fedora 系:用
sudo yum install libjpeg-devel libpng-devel libtiff-devel freetype-devel lcms2-devel libwebp-devel zlib-devel(Fedora 改用dnf) - macOS(M1/M2/M3):先确认 Xcode Command Line Tools 已装(
xcode-select --install),再用 Homebrew 装依赖:brew install libjpeg libpng libtiff freetype lcms2 webp - 装完依赖后,**不要**直接
pip install Pillow—— 先清缓存:pip cache purge,再重装,避免 pip 复用之前失败的构建缓存
PyCharm / VS Code 中装了 Pillow 却 still 报 No module named 'PIL'
这是环境错位最典型的症状:你装的 Pillow 在 A 环境,但编辑器运行代码用的是 B 环境。尤其多 Python 版本、虚拟环境未激活、conda/pip 混用时高频发生。
- 在终端里验证:打开项目所在目录的终端,运行
which python(macOS/Linux)或where python(Windows),再运行python -m pip list | grep -i pillow(或findstr pillow),确认 Pillow 是否真在该 Python 下 - PyCharm:File → Settings → Project → Python Interpreter,看右上角显示的 interpreter 路径是否和终端一致;若不一致,点击齿轮图标 → Add → Existing environment,选中你刚确认的那个
python可执行文件 - VS Code:按 Ctrl+Shift+P → “Python: Select Interpreter”,选和终端一致的路径;装完后务必重启终端集成窗口(Ctrl+Shift+`)
- 切记:
import pillow是错的,正确写法永远是from PIL import Image—— Pillow 安装后提供的是PIL命名空间,不是pillow模块名
最容易被忽略的一点:Pillow 版本和 Python 版本必须匹配。比如 Python 3.12 只能装 Pillow 10.0+,装 9.x 会静默失败或导入异常;反过来,旧项目用 Python 3.7,硬上 Pillow 10.0 就会报 ImportError: cannot import name 'Image' from 'PIL'。查版本兼容表比反复重装更省时间。


















