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

easy_install PIL

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

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

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


当前回答

pip(3) uninstall Pillow
pip(3) uninstall PIL
pip(3) install Pillow

其他回答

有时候我在python中运行Unitest时会遇到这种错误。解决方案是卸载并在虚拟环境中安装相同的包。

使用命令:

pip uninstall PIL

and

pip install  PIL 

如果由于任何原因出现错误,请在命令的开头添加sudo,并在按enter键后输入密码。

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

步骤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

我在导入PIL和进一步导入ImageTk和Image模块时遇到了同样的问题。我也尝试过直接通过pip安装PIL。但成功不可能实现。在这两者之间,有人建议PIL已弃用,因此,试图通过pip只安装枕头。枕头安装成功,PIL包在python27/Lib/site-packages/路径下。

现在Image和ImageTk都可以导入了。

我使用:

pip install Pillow 

和pip在Lib\site-packages中安装PIL。当我把PIL搬到Lib时,一切都很好。我用的是Windows 10。

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

conda install --channel conda-forge pillow=5

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