PIL:ImportError:_imaging扩展为其他版本的pillow或PIL构建
我遇到了这个错误:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-4-0f6709e38f49> in <module>()
----> 1 from PIL import Image
C:\Anaconda\lib\site-packages\PIL\Image.py in <module>()
61 from PIL import _imaging as core
62 if PILLOW_VERSION != getattr(core, 'PILLOW_VERSION', None):
---> 63 raise ImportError("The _imaging extension was built for another "
64 " version of Pillow or PIL")
65
ImportError: The _imaging extension was built for another version of Pillow or PIL
每当我尝试使用PIL库时就会出现这个问题。我正在尝试加载和处理一堆.gif格式的图片,现在我尝试的是以下内容:
from PIL import Image
我换了个方法,使用scipy来做:
import scipy.ndimage as spnd
os.chdir('C:\\WeatherSink\\data\\')
spnd.imread('2014-11-03-0645.gif')
结果失败了,显示:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-3-23c383b79646> in <module>()
1 os.chdir('C:\\WeatherSink\\data\\')
----> 2 spnd.imread('2014-11-03-0645.gif')
C:\Anaconda\lib\site-packages\scipy\ndimage\io.pyc in imread(fname, flatten, mode)
36 from PIL import Image
37 except ImportError:
---> 38 raise ImportError("Could not import the Python Imaging Library (PIL)"
39 " required to load image files. Please refer to"
40 " http://pypi.python.org/pypi/PIL/ for installation"
ImportError: Could not import the Python Imaging Library (PIL) required to load image files. Please refer to http://pypi.python.org/pypi/PIL/ for installation instructions.
第一个方法让我关注安装的PIL版本。我尝试模拟getattr(...),结果返回了None。所以我并不惊讶它的功能不太正常。有人知道怎么“修复”这些错误吗?
我在win7系统上运行,使用conda管理python2.7。我也尝试过删除并重新安装这些包,但输出没有任何变化。
非常感谢大家的帮助。
6 个回答
我觉得其实是你在运行代码时使用的虚拟环境。很多时候,电脑会运行Anaconda的路径,而不是Python3。所以在调用你的代码之前,你可以在命令提示符中试试输入python或者python3。比如:python3 image.py。或者你也可以直接删除Anaconda :D。
这个问题是因为你电脑上的Python库PIL或Pillow的版本和你的系统不匹配,所以才会出现这个问题。
你可以试试这个命令:
sudo apt-get install python-PIL
检查一下这个库是否已经安装。如果已经安装了,可以用下面的命令把它卸载:
sudo apt-get remove python-PIL
这个命令可以帮助你把PIL/Pillow库从你的系统中删除。
最后,下面这个命令可以帮助你解决这个库的问题:
sudo apt-get autoremove python-PIL
然后重新安装PIL/Pillow库:
sudo apt-get install python-pil
这样应该能帮助你解决问题。
可能你的某个依赖项需要用到PIL,而PIL是在Pillow之后安装的,这就导致了你在网站包目录里出现了冲突。我猜测你看到那个错误是因为导入语句从一个正常的PIL安装中导入了_imaging
,而不是从Pillow的安装中导入。
我以前也遇到过需要PIL或Pillow的包之间的冲突。Pillow当然是更推荐的包。我建议你查看一下你所用包的依赖关系。如果你能找到一个依赖于PIL的包,可以提交一个请求,把依赖改成Pillow,或者干脆自己创建一个分支来做这个改动。对我来说,创建分支是我最终选择的方案,因为那个项目似乎很久没有更新了。
总之,你要尽量消除对PIL包的依赖(因为它已经不再活跃),转而使用Pillow。
这是一个关于Python 3.6的问题。你需要编辑一个文件,路径是:C:\Anaconda\lib\site-packages\PIL\Image.py
,然后修改里面的代码:
if PILLOW_VERSION != getattr(core, 'PILLOW_VERSION', None):
raise ImportError("The _imaging extension was built for another "
" version of Pillow or PIL")
把它改成:
if core.PILLOW_VERSION != getattr(core, 'PILLOW_VERSION', None):
raise ImportError("The _imaging extension was built for another "
" version of Pillow or PIL")
这样就能解决这个问题了。祝好!
这只是一个安装的问题。
首先,如果你的系统上还没有安装pip,就先安装它。它在Windows上也可以使用。
接下来,升级你的numpy、pip/pillow和scipy:
pip install -U numpy
pip install -U pil/pillow
pip install -U scipy
对于Windows来说,最好的选择是使用anaconda。
我觉得在conda里pip已经安装好了。这可以解决你系统版本的问题。
In [1]: from PIL import Image
In [2]: import scipy.ndimage as spnd
In [3]: x = spnd.imread('ppuf100X91.gif')
In [4]: print x
[[255 255 255 ..., 255 255 255]
[255 255 255 ..., 255 255 255]
[255 255 255 ..., 255 255 255]
...,
[255 255 255 ..., 255 255 255]
[255 255 255 ..., 255 255 255]
[255 255 255 ..., 255 255 255]]