简体   繁体   English

安装了枕头,但仍收到“ ImportError:无名为PIL的模块”

[英]Have pillow installed and yet getting 'ImportError: No module named PIL'

Pillow was working fine for me and then I got this error : 枕头对我来说很好,然后我收到此错误:

File "/usr/local/lib/python2.7/site-packages/PIL/Image.py", line 2452, in open  
    % (filename if filename else fp))  
IOError: cannot identify image file 'dataSet/.DS_Store'

From answers on SO I got that it is possible I have both PIL and pillow installed and so I uninstalled PIL and installed pillow using easy_install and ever since then I keep getting this error. 从答案上我得到了可能同时安装了PIL和枕头,因此我使用easy_install卸载了PIL并安装了枕头,从那时起,我一直收到此错误。

ImportError: No module named PIL

I tried everything. 我尝试了一切。

pip install image  
pip install pillow  
pip upgrade pip  
import PIL from Image  

pip --version gives : pip 9.0.1 from /usr/local/lib/python2.7/site-packages (python 2.7) pip --version给出: pip 9.0.1 from /usr/local/lib/python2.7/site-packages (python 2.7)
pip freeze | grep Pillow pip freeze | grep Pillow gives : Pillow==4.1.0 pip freeze | grep Pillow给出: Pillow==4.1.0

The code I'm running is: 我正在运行的代码是:

import os
import cv2
import numpy as np 
from PIL import Image

And at line 4 I keep getting the error 在第4行,我不断收到错误消息

Traceback (most recent call last):  
  File "finder.py", line 4, in <module>  
    from PIL import Image  
ImportError: No module named PIL

What else can I do to fix the situation? 我还能采取什么措施来解决这种情况? Did I read all the posts related to this topic on SO? 我是否阅读了与该主题相关的所有帖子? Pretty much. 差不多了 And there are MANY posts related to it as well,but please,help me. 并且也有许多与此相关的帖子,但是请帮助我。

It seems that You have Installed Pillow for python3.* pip install pillow Install the right version for python2.7 from here https://pypi.python.org/pypi/Pillow/4.0.0 似乎您已经为python3 pip install pillow 。* pip install pillow从这里https://pypi.python.org/pypi/Pillow/4.0.0安装正确的python2.7版本。

python2.7 version has cp27 in it's name, download the right wheel file based on your system architecture. python2.7版本的名称为cp27 ,请根据您的系统架构下载正确的wheel文件。 pip install some-package.whl I hope it helps. pip install some-package.whl希望对您pip install some-package.whl帮助。

Regarding the first error, this is an expected error: 关于第一个错误,这是预期的错误:

File "/usr/local/lib/python2.7/site-packages/PIL/Image.py", line 2452, in open
% (filename if filename else fp))
IOError: cannot identify image file 'dataSet/.DS_Store'

You're trying to open a non-image with Pillow, and it's responding by saying it cannot open it. 您正在尝试使用Pillow打开非图像,并且它的回应是说它无法打开它。 This is correct behaviour. 这是正确的行为。

I guess you're trying to open all the images in a directory, but it's failing on .DS_Store , which aa metadata file created by macOS when viewing the contents of the directory. 我猜您正在尝试打开目录中的所有图像,但是在.DS_Store上失败,这是macOS在查看目录内容时创建的元数据文件。 This file won't have existed before you looked in the directory with Finder, hence the code worked. 在使用Finder查找目录之前,该文件将不存在,因此代码可以正常工作。 What you need to do is either ignore this file (as you would with Thumbs.db on Windows), be more careful with globbing (eg. make sure only .jpg or .png or such) or handle the IOError with a try: / except IOError: block. 您需要做的是要么忽略此文件(就像在Windows上使用Thumbs.db一样),请更小心地进行遍历(例如,确保仅.jpg或.png等),或try:使用IOError try: except IOError:except IOError:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM