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

easy_install PIL

然后我运行python并键入:import PIL。但是我得到了这个错误:

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

我从来没有遇到过这样的问题,你觉得呢?


当前回答

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

然后我想起我安装了多个版本的Python并且我使用PyCharm IDE(这是我得到这个错误消息的地方),所以在我的情况下的解决方案是:

在PyCharm:

进入“File>Settings>Project>Python Interpreter”

点击“+”(安装)

从列表中找到Pillow并安装它

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

其他回答

在windows 10上,我设法做到了:

cd "C:\Users\<your username>\AppData\Local\Programs\Python\Python37-32" 
python -m pip install --upgrade pip     <-- upgrading from 10.something to 19.2.2.
pip3 uninstall pillow
pip3 uninstall PIL
pip3 install image

之后在python (python 3.7在我的情况下),这工作得很好…

import PIL
from PIL import image

我使用conda-forge安装枕头版本5,这似乎对我有用:

conda install --channel conda-forge pillow=5

正常的conda安装枕头不为我工作。

解决这个问题最干净的方法是遵循以下步骤。

步骤1:卸载PIL包

pip uninstall PIL

第二步:在windows操作系统上使用pip安装枕头,如下图所示。对于其他环境,请检查文章No module named PIL

在Windows上

python3 -m pip install --upgrade pip
python3 -m pip install --upgrade Pillow

步骤3:Python Imaging Library中最重要的类是Image类,您可以如下所示导入它。

from PIL import Image
im = Image.open("myimage.jpg")

如果成功,该函数返回一个Image对象。你现在可以使用实例属性来检查文件内容:

print(im.format, im.size, im.mode)

#Output: PPM (512, 512) RGB

首先安装枕头与

pip install Pillow

或者如下

c:\Python35>python -m pip install Pillow

然后在python代码中调用

from PIL import Image

Pillow是PIL的一个分支,PIL是Python映像库,它不再被维护。但是,为了保持向后兼容性,使用旧的模块名称。”从枕头安装,但“没有模块命名枕头”- python2.7 - Windows 7 - python -m安装枕头

在windows上,尝试检查PIL库位置的路径。在我的系统里,我注意到路径是

\Python26\Lib\site-packages\pil instead of \Python26\Lib\site-packages\PIL  

在将pil文件夹重命名为pil之后,我就可以加载pil模块了。