2014-11-03 91 views
14

的另一個版本內置我得到的錯誤:PIL:導入錯誤:將_imaging擴展是爲枕頭或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。我試圖刪除並重新安裝這些軟件包,而不會改變輸出。

非常感謝幫助。

+1

首先猜測你是否安裝了多個版本的PIL或Pillow,並且可能在不同的環境下安裝。找到他們全部,全部卸載它們,然後重新安裝一個新的枕頭。 – Hugo 2014-11-04 10:14:13

回答

3

也許你的一個依賴項需要PIL和PIL最終在Pillow之後安裝,導致你的站點包dir中出現衝突。我假設你看到了這個錯誤,因爲導入語句是從合法的PIL安裝導入_imaging而不是Pillow安裝。

我在過去遇到過需要PIL或Pillow的衝突包的麻煩。枕頭當然是首選套餐。我會看看你的包的依賴關係。如果你能找到一個依賴於PIL的函數,我會提交一個pull請求,它將依賴關係改變爲Pillow,或者甚至可以用這個改變創建你自己的fork。對於我的情況,分叉是我選擇的選擇,因爲該項目似乎很長一段時間沒有任何活動。

最終,您希望消除對PIL包的任何依賴關係(因爲它不再處於活動狀態)以支持Pillow。

+0

http://scikit-learn.org/stable/auto_examples/applications/face_recognition.html – 2014-12-31 22:45:53

+3

Jan,我應該在該頁面上尋找什麼? – 2014-12-31 22:47:42

+0

是的,請:) :) – 2014-12-31 22:53:23

16

這只是一個安裝問題。 在你的系統首先安裝PIP如果不是insalled它可用於Windows也https://pip.pypa.io/en/latest/installing.html

升級numpy的,PIP /枕頭,SciPy的

PIP安裝-U numpy的

PIP安裝-U PIL /枕頭

PIP安裝-U SciPy的

或最好的選擇窗口是使用anaconda

我認爲pip已經安裝在conda中。這將解決您的系統版本問題。

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]] 
+1

它解決了我的問題,thx!我不得不使用'pip install -U pillow''('pip install -U pil'給''找不到滿足需求pil'的版本)。 – 2016-01-22 16:16:34

3

這個問題是因爲Python包PIL /枕頭是向上或系統的縮小版本,並且由於這個問題是在你的系統中生成。

嘗試檢查這個命令:

sudo apt-get install python-PIL 

檢查這個包是安裝或不安裝。如果安裝不是試圖用命令刪除:

sudo apt-get remove python-PIL 

入住這將努力消除在PIL /枕頭包從系統中刪除。

最後這個命令將幫助你解決這個問題包:

sudo apt-get autoremove python-PIL 

然後重新安裝PIL /枕頭包:

sudo apt-get install python-pil 

這會幫助你解決問題。

1

這是在Python 3.6 編輯文件的一個問題:C:\蟒蛇\ LIB \站點包\ PIL \ Image.py 和更改代碼: 如果PILLOW_VERSION = GETATTR(核心, 'PILLOW_VERSION',無! ): 提高導入錯誤(以下簡稱 「_imaging擴展建另一個」 「版本枕頭或PIL的」)

變化,爲: 如果core.PILLOW_VERSION = GETATTR(核心, 'PILLOW_VERSION',無)! raise ImportError(「_imaging擴展是爲另一個」 「版本的Pillow或PIL構建的)

這將解決問題。 Regards

+0

嗨易卜拉欣,這是否也適用於Python 2.7?你能否強調兩個碼位之間的區別? – 2017-05-05 13:16:22

+0

是的,在python 2.7中,它有同樣的問題。問題是它導入了所有帶有名稱空間核心的符號,並且在沒有名稱空間的情況下尋址常量 – ibrahim 2017-05-07 02:01:00