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

同虚拟环境下.py可运行但Jupyter Notebook报Pillow DLL加载错误的解决方法

解决Jupyter Notebook中Pillow _imaging DLL加载失败问题

同一虚拟环境下,以下代码在.py文件中可正常执行:

import torchvision
train_set = torchvision.datasets.CIFAR10(root="./mydata", train=True, download=True)
test_set = torchvision.datasets.CIFAR10(root="./mydata", train=False, download=True)

但在Jupyter Notebook运行时触发报错:

ImportError: DLL load failed while importing _imaging: 操作系统无法运行 %1。

已确认Pillow版本与PyTorch匹配,尝试过添加路径的方法,但并非每次有效:

import os
import sys

# 添加 Python DLL 和 site-packages 目录(Pillow 的 _imaging.pyd 依赖 DLL)
python_path = sys.exec_prefix
os.environ["PATH"] = (
    python_path + r"\Library\bin;" +
    python_path + r"\Library\lib;" +
    python_path + r"\Lib\site-packages\PIL;" +
    os.environ["PATH"]
)

# 再导入 Pillow
from PIL import Image
print("Pillow version:", Image.__version__)

完整报错回溯:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[6], line 1
----> 1 from PIL import Image
      2 print(Image.__version__)

File C:\ProgramData\miniconda3\envs\dl_env\Lib\site-packages\PIL\Image.py:90
     81 MAX_IMAGE_PIXELS: int | None = int(1024 * 1024 * 1024 // 4 // 3)
     84 try:
     85     # If the _imaging C module is not present, Pillow will not load.
     86     # Note that other modules should not refer to _imaging directly;
     87     # import Image and use the Image.core variable instead.
     88     # Also note that Image.core is not a publicly documented interface,
     89     # and should be considered private and subject to change.
---> 90     from . import _imaging as core
     92     if __version__ != getattr(core, "PILLOW_VERSION", None):
     93         msg = (
     94             "The _imaging extension was built for another version of Pillow or PIL:\n"
     95             f"Core version: {getattr(core, 'PILLOW_VERSION', None)}\n"
     96             f"Pillow version: {__version__}"
     97         )

ImportError: DLL load failed while importing _imaging: 操作系统无法运行 %1。

彻底解决方法

1. 绑定Jupyter到正确的虚拟环境

  • 激活目标虚拟环境:conda activate dl_env
  • 安装ipykernel:conda install ipykernel 或 pip install ipykernel
  • 将虚拟环境添加为Jupyter内核:python -m ipykernel install --user --name dl_env --display-name "Python (dl_env)"
  • 重启Jupyter Notebook,选择对应内核运行代码

2. 重新编译适配环境的Pillow

  • 卸载现有Pillow:pip uninstall -y pillow
  • 安装编译依赖(Windows环境):conda install -c conda-forge libjpeg-turbo zlib
  • 从源码编译安装:pip install --no-binary :all: pillow
    该操作会根据当前系统环境编译Pillow,确保_imaging模块与本地DLL完全兼容

3. 修复缺失的系统DLL依赖

  • 使用Dependency Walker工具打开虚拟环境下的_imaging.pyd文件(路径示例:C:\ProgramData\miniconda3\envs\dl_env\Lib\site-packages\PIL\_imaging.pyd)
  • 查看工具提示的缺失DLL,将对应文件复制到虚拟环境的Library\bin目录,或把DLL所在路径添加到系统PATH环境变量
  • 重启电脑后重新测试

4. 重置Jupyter配置

  • 执行jupyter --config-dir找到配置文件路径,删除该目录下所有文件
  • 重新启动Jupyter Notebook,加载干净配置

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 15:05:04