You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Raspbian下Python3.9调用PIL字体报_imagingft C模块未安装错误

Raspbian系统Pillow加载字体报_imagingft C module is not installed修复方案

问题现象

在Raspbian系统Python 3.9环境下使用PIL(Pillow)库加载自定义TrueType字体时,持续触发导入错误,完整报错栈如下:

Traceback (most recent call last):
  File "/home/pi/python/src/mintest.py", line 2, in <module>
    font52 = ImageFont.truetype('fonts/Font.ttf', 52)
  File "/home/pi/.local/lib/python3.9/site-packages/PIL/ImageFont.py", line 878, in truetype
    return freetype(font)
  File "/home/pi/.local/lib/python3.9/site-packages/PIL/ImageFont.py", line 875, in freetype
    return FreeTypeFont(font, size, index, encoding, layout_engine)
  File "/home/pi/.local/lib/python3.9/site-packages/PIL/ImageFont.py", line 196, in __init__
    if core.HAVE_RAQM:
  File "/home/pi/.local/lib/python3.9/site-packages/PIL/ImageFont.py", line 70, in __getattr__
    raise ImportError("The _imagingft C module is not installed")
ImportError: The _imagingft C module is not installed

最小复现场景

仅需两行代码即可稳定复现问题:

from PIL import ImageFont   
font52 = ImageFont.truetype('fonts/Font.ttf', 52)

基础环境信息

Distributor ID: Raspbian
Description: Raspbian GNU/Linux 9.13 (stretch)
Release: 9.13
Codename: stretch
Python 版本: 3.9.6

执行python3 -m PIL可查看当前环境Pillow的模块支持、编译配置详情,所有公开可检索的常规方案尝试后问题仍未解决。

根因说明

该报错本质是当前安装的Pillow版本没有编译出_imagingft动态链接模块,核心诱因是Raspbian Stretch为旧稳定版,自行安装的Python3.9不在系统默认源适配范围内,通过pip安装Pillow时要么下载了不兼容的预编译wheel包,要么系统缺少FreeType等必要开发依赖,导致编译过程跳过了freetype相关模块的构建。

修复步骤

按顺序执行以下操作即可解决问题:

  • 安装Pillow编译所需的全部系统级依赖
sudo apt update
sudo apt install -y libfreetype6-dev libjpeg-dev zlib1g-dev liblcms2-dev libwebp-dev libharfbuzz-dev libfribidi-dev
  • 彻底卸载当前环境中残缺的Pillow版本,清除安装缓存避免复用错误的编译产物
pip3.9 uninstall -y pillow
pip3.9 cache purge
  • 强制从源码编译安装Pillow,禁用预编译wheel包,确保编译过程能正确检测到系统中安装的FreeType库
pip3.9 install --no-binary :all: pillow

安装过程注意观察终端输出的配置汇总表,确认freetype选项显示为yes,若显示no说明依赖安装异常,回头检查libfreetype6-dev的安装状态

  • 验证修复结果,执行以下测试代码无报错即修复完成
from PIL import ImageFont
# 可替换为自己的字体文件路径
test_font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 52)
print("字体加载正常,_imagingft模块可用")

如果上述步骤执行后仍报错,检查Python3.9是否为自行编译安装:若编译Python时未加--enable-shared参数,会导致动态库链接异常,需要重新编译Python3.9,编译时加上该参数,配置好ldconfig动态库路径后再重新执行上述Pillow安装步骤即可。

内容的提问来源于stack exchange,提问作者driconmax

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 02:18:19