导入错误:找不到PIL模块。

360

我在 shell 中使用这个命令来安装 PIL:

easy_install PIL

我运行python并输入以下命令:import PIL。但是我遇到了下面的错误:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: No module named PIL

我从未遇到过这样的问题,你认为是什么原因?


16
这篇文档页面实际上建议使用from PIL import Image,但这种方法不起作用。 - Janne Karila
2
@Janne 只需使用Import Image,PIL就可以开始工作了。 - Asma Gheisari
2
此时我建议使用easy_install Pillow。Pillow是PIL的一个分支,具有更好的打包和支持Python 3的功能。 - Lennart Regebro
1
你能否将被接受的答案更改为推荐Pillow的答案?正如@LennartRegebro所说,这个包在长期内更好。 - Josiah Yoder
2
请使用 pip install Pillow 命令进行安装。然后您就可以使用 import PIL.Image 进行导入。 - Tiago Martins Peres
31个回答

1

我使用conda-forge安装了Pillow 5版本,这对我来说似乎起作用了:

conda install --channel conda-forge pillow=5

常规的conda安装pillow对我来说没有起作用。

1
我遇到了同样的问题,通过检查 pip 的版本(pip3 --version),我发现我输入的是 python<错误版本> filename.py 而不是 python<正确版本> filename.py,于是我解决了这个问题。

1

我找到了一个更简单的解决方案。使用虚拟环境。

pip install Pillow

from PIL import Image

在我的 macOS 上这个有效


1
我使用了:


from pil import Image

替代

from PIL import Image

并且对我来说很好用

祝你一切顺利


0

我最近安装了Leap。我尝试打开Openshot,但它没有启动。所以我来到这里,并找到一个建议从终端开始查看是否有任何错误。

我遇到的错误是error missing mlt。所以我从Yast安装了python-mlt模块并导入它,尝试启动,但接下来Openshot说missing pil

我按照Pillow的建议进行了安装,因为Yast找不到任何pil并导入了pil。那个还好,但是没有启动并显示Error missing goocanvas

然后我使用Yast安装了goocanvas,在Python中导入它,然后Openshot就启动了!

在终端中出现了很多错误,例如missing Vimeoclient和大量的attributeerrors。好吧,我们将看看它是否对工作产生任何影响。


0

在导入PIL并进一步导入ImageTk和Image模块时,我遇到了同样的问题。我还尝试直接通过pip安装PIL,但没有成功。因为有人建议PIL已经过时,所以我尝试只通过pip安装pillow。pillow已成功安装,然后在路径python27/Lib/site-packages/下创建了PIL包。

现在可以导入Image和ImageTk两个模块了。


0

我曾经遇到过同样的问题,并尝试了上面列出的许多解决方案。

然后我记起自己安装了多个 Python 版本,并且使用了 PyCharm IDE(这就是我得到这个错误消息的地方),所以我的解决方案是

在 PyCharm 中:

转到 文件>设置>项目>Python 解释器

点击“+”(安装)

从列表中找到 Pillow 并安装它

希望这能帮助到可能处于类似情况下的任何人!


0
根据官方网站安装Pillow,您可以尝试以下步骤:
打开终端并运行:
  • python3 -m pip install --upgrade pip
然后运行:
  • source ~/.bash_profile

0
在Debian bookworm上的解决方法是运行以下命令:
sudo apt-get install python3-pil

0

在 Windows 10 上使用 Python 3.8。答案的组合对我有用。请参见下面的独立工作示例。应在命令行中执行已注释的行。

import requests
import matplotlib.pyplot as plt
# pip uninstall pillow
# pip uninstall PIL
# pip install image
from PIL import Image
url = "https://images.homedepot-static.com/productImages/007164ea-d47e-4f66-8d8c-fd9f621984a2/svn/architectural-mailboxes-house-letters-numbers-3585b-5-64_1000.jpg"
response = requests.get(url, stream=True)
img = Image.open(response.raw)
plt.imshow(img)
plt.show()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接