Python:Could not find a version that satisfies the requirement PIL 报错解决

解释 pip install PIL 失败的原因:PIL 已停更,Python 3 应安装 Pillow(pip install pillow)但代码中仍用 from PIL import Image 导入;附安装、示例代码与避坑提醒。


项目场景:

刚接触Python图像处理的开发者,在学习教程时看到“使用PIL库处理图片”的指引,想按照样学样实现一个简单的图片裁剪功能。按照常规思路,先通过pip安装库,再在代码中导入使用——但这个过程中却遇到了一系列问题。

问题描述

  1. 首先根绝代码里面的

    from PIL import image
    

    想要直接下载PIL包

  2. 按照“安装PIL库”的想法,在命令行执行安装命令:

    pip install PIL
    

    结果报错,提示“找不到满足要求的PIL版本”(Could not find a version that satisfies the requirement PIL )。

原因分析:

这个问题的核心是PIL和Pillow的“历史渊源”与命名差异

  1. PIL
    PIL(Python Imaging Library)是Python最早的图像处理库,诞生于1995年,功能强大但仅支持Python 2.x。2011年后停止更新,逐渐无法兼容Python 3,逐渐被淘汰。

  2. Pillow
    由于PIL的停滞,社区在PIL的基础上fork(分支)出了Pillow项目,目的是:

    • 支持Python 3.x及后续版本
    • 修复BUG并持续维护
    • 扩展更多功能(如支持WebP、HEIF等新型格式)
  3. 命名的“妥协”
    为了兼容大量基于PIL编写的旧代码,Pillow在设计时保留了PIL作为导入名(即from PIL import ...),但安装包名改为pillow(避免与原始PIL冲突)。
    这就导致了“安装用pillow,导入用PIL”的“看似矛盾”的现象。

解决方案:

将pip安装的时候名字由PIL换为pillow即可,导入的时候仍然使用 PIL

1. 正确安装库

只能用pillow作为包名安装,这是官方指定的安装方式:

# 安装最新版本
pip install pillow

# 如需指定版本(例如9.5.0)
pip install pillow==9.5.0

2. 正确导入使用

代码中必须用PIL作为导入前缀,这是为了兼容历史代码:

# 正确示例:打开并显示一张图片
from PIL import Image

# 打开图片(替换为你的图片路径)
img = Image.open("test.jpg")

# 显示图片基本信息
print(f"图片格式:{img.format}")  # 如 JPEG、PNG
print(f"图片尺寸:{img.size}")    # 如 (1920, 1080)

# 简单处理:裁剪图片(左、上、右、下坐标)
cropped_img = img.crop((100, 100, 500, 500))

# 保存处理后的图片
cropped_img.save("cropped_result.png")

3. 避坑提醒

  • 不要尝试安装PIL(早已废弃,无法在Python 3环境使用)。
  • 不要在代码中写from pillow import ...(导入名永远是PIL)。
  • 若环境中同时存在旧版PIL和Pillow,可能导致冲突,建议卸载PIL后重新安装Pillow:
    pip uninstall PIL  # 如有旧版本
    pip uninstall pillow
    pip install pillow
    

总结来说:Pillow是PIL的“现代继承者”,安装时用pillow,使用时用PIL——记住这个“安装名≠导入名”的特殊情况,就能轻松避开这个新手常见的坑。

Python:Could not find a version that satisfies the requirement PIL 報錯解決

解釋 pip install PIL 失敗的原因:PIL 已停更,Python 3 應安裝 Pillow(pip install pillow)但代碼中仍用 from PIL import Image 導入;附安裝、示例代碼與避坑提醒。

來源:https://blog.csdn.net/2403_87969572/article/details/151948371

擷取時間(ISO 本地):2026-05-18 05:17:02


專案場景

剛接觸 Python 影像處理的開發者,在學習教學時看到「使用 PIL 函式庫處理圖片」的指引,想按照樣學樣實現一個簡單的圖片裁剪功能。按照常規思路,先透過 pip 安裝函式庫,再在程式碼中匯入使用——但這個過程中卻遇到了一系列問題。

問題描述

  1. 首先根據程式碼裡面的 from PIL import Image 想要直接下載 PIL 包
  2. 執行 pip install PIL 結果報錯:Could not find a version that satisfies the requirement PIL

原因分析

核心是 PIL 和 Pillow 的「歷史淵源」與命名差異

  1. PIL:僅支援 Python 2.x,2011 年後停更。
  2. Pillow:社群 fork,支援 Python 3、持續維護、擴展格式。
  3. 命名妥協:安裝包名 pillow,匯入名仍為 PIL

解決方案

安裝時用 pillow,匯入時仍用 PIL

1. 正確安裝函式庫

pip install pillow
pip install pillow==9.5.0

2. 正確匯入使用

from PIL import Image

img = Image.open("test.jpg")
print(f"圖片格式:{img.format}")
print(f"圖片尺寸:{img.size}")

cropped_img = img.crop((100, 100, 500, 500))
cropped_img.save("cropped_result.png")

3. 避坑提醒

  • 不要嘗試安裝 PIL(Python 3 已無法使用)。
  • 不要寫 from pillow import ...
  • 若衝突可卸載後重裝 Pillow。

總結:安裝用 pillow,使用時用 PIL——記住「安裝名≠匯入名」即可避開新手常見的坑。

Python Fix: Could not find a version that satisfies the requirement PIL

Beginners following “use PIL for images” try pip install PIL after from PIL import Image and get Could not find a version that satisfies the requirement PIL.

Captured at (local ISO): 2026-05-18 05:17:02


Scenario

Beginners following “use PIL for images” try pip install PIL after from PIL import Image and get Could not find a version that satisfies the requirement PIL.

Cause

PIL vs Pillow history:

  1. PIL (Python Imaging Library): classic library, Python 2 only, unmaintained since ~2011.
  2. Pillow: community fork for Python 3+, bugfixes, new formats (WebP, HEIF, …).
  3. Naming: install package pillow, but import namespace stays PIL for backward compatibility.

Solution

Install pillow, import as PIL.

1. Install

pip install pillow
pip install pillow==9.5.0

2. Import & use

from PIL import Image

img = Image.open("test.jpg")
print(f"Format: {img.format}")
print(f"Size: {img.size}")

cropped_img = img.crop((100, 100, 500, 500))
cropped_img.save("cropped_result.png")

3. Pitfalls

  • Do not pip install PIL on Python 3.
  • Do not from pillow import ... — always from PIL import ....
  • If old PIL and Pillow conflict:
    pip uninstall PIL
    pip uninstall pillow
    pip install pillow

Summary: Pillow is PIL’s modern successor—install pillow, use PIL in code.