欢迎各位兄弟 发布技术文章
这里的技术是共享的
Traceback (most recent call last):
File "D:\pythonProject4\self_installation\self_installation_ok.py", line 3, in <module>
from PIL import Image, ImageTk
ModuleNotFoundError: No module named 'PIL'
这个错误是因为你的 Python 环境中没有安装 Pillow
库(它提供了 PIL
模块)。
安装 Pillow
在终端或命令行中运行以下命令:
bashpip install Pillow
如果你使用的是 Python 3,并且 pip
不生效,可以尝试:
bashpip3 install Pillow
检查虚拟环境(如果有)
如果你使用了虚拟环境(如 venv
或 conda
),请先进入虚拟环境再安装。
例如,使用 conda
:
bashconda install pillow
验证安装
安装完成后,可以运行以下命令检查是否成功:
bashpython -c "from PIL import Image; print('Pillow installed successfully!')"
如果没报错,说明安装成功。
重新运行你的脚本
bashpython self_installation_ok.py
权限问题:如果安装失败,尝试加上 --user
:
bashpip install --user Pillow
网络问题:可以使用国内镜像加速安装(如清华源):
bashpip install Pillow -i https://pypi.tuna.tsinghua.edu.cn/simple
安装完成后应该就能正常使用 from PIL import Image, ImageTk
了!