python艱辛安裝過程,ImportError:No module named PIL

一、安裝python

在網上很快找到並下載了python2.7版本的安裝包。Python 2.7是一個很穩定,成熟的版本,而且有很多開源的模塊。PIL就是其中一個非常重要的模塊。

Python2.7.10的下載地址爲:


http://www.xiazaiba.com/html/2049.html

下載文件爲python_2.7.10_XiaZaiBa.rar,解壓後雙擊python-2.7.10.msi,使用默認配置安裝即可。最後會在C盤出現Python27的文件夾。

打開dos窗口,切換到C:\Python27,輸入python命令,出現如下字符表示安裝成功。

 

在上面的窗口,輸入import PIL

則會出現如下字符:

>>>import PIL

Traceback(most recent call last):

 File "<stdin>", line 1, in<module>

ImportError:No module named PIL

表示安裝的python2.7.10中默認並沒有安裝PIL模塊。接下來,我們需要先安裝PIL模塊。

二、PIL版本選擇

從網上搜索得知,PIL官方只有32位的安裝文件,安裝時會提示找不到python的安裝路徑。64Win7下無法安裝PIL庫的原因是:PIL官方http://www.pythonware.com/products/pil/提供的PIL二進制安裝庫包都是32位的。64位程序和32位程序檢測註冊表的位置是不一樣的:64位程序檢測HKEY_LOCAL_MACHINESOFTWAREPython,而32位程序檢測HKEY_LOCAL_MACHINESOFTWAREWow6432NodePython。如果安裝的python64位的,其相關信息都在HKEY_LOCAL_MACHINESOFTWAREPython下面,而32位程序則在HKEY_LOCAL_MACHINESOFTWAREWow6432NodePython下面找Python安裝信息,結果找不到了,所以會報錯。

網上有非官方的64位庫(官方源碼編譯版),叫做pillowPillowPIL的替代版本。其網址爲:http://www.lfd.uci.edu/~gohlke/pythonlibs/

在這個網站上有6個版本,分別爲Pillow-3.1.0-cp27-none-win32.whlPillow-3.1.0-cp27-none-win_amd64.whlPillow-3.1.0-cp34-none-win32.whlPillow-3.1.0-cp34-none-win_amd64.whlPillow-3.1.0-cp35-none-win32.whlPillow-3.1.0-cp35-none-win_amd64.whl

根據我當前的情況,我下載了Pillow-3.1.0-cp27-none-win32.whl。這個文件並非可執行文件,從網站上介紹,需要使用PIP工具安裝。

三、安裝PIP

PIP是一個安裝和管理Python包的工具 , easy_install的一個替換品。在python中需要使用easy_install來安裝PIP。所以,我們首先要確認easy_install是否可用。

在目錄C:\Python27\Scripts下會看到easy_install的一些相關文件。在dos窗口,將目錄切換到C:\Python27\Scripts下,輸入easy_install,如果看到如下字符表示easy_install工具可以正常使用:

C:\Python27\Scripts>easy_install

error: Nourls, filenames, or requirements specified (see --help)

在目錄C:\Python27\Scripts下也會看到PIP相關的文件。然後在當前窗口使用easy_install安裝PIP

C:\Python27\Scripts>easy_install.exe pip

通過這個命令即可啓動PIP的安裝。安裝完畢,輸入pip命令,則會出現如下字符:

C:\Python27\Scripts>pip

 

Usage:

  pip <command> [options]

 

Commands:

  install                     Install packages.

  download                    Download packages.

  uninstall                   Uninstall packages.

  freeze                      Output installed packagesin requirements format.

  list                        List installed packages.

  show                        Show information aboutinstalled packages.

  search                      Search PyPI for packages.

  wheel                       Build wheels from yourrequirements.

  hash                        Compute hashes of packagearchives.

  help                        Show help for commands.

當我使用PIP安裝PIL庫的時候,提示pip版本比較舊,需要更新版本:

C:\Python27\Scripts>pip install Pillow-3.1.0-cp27-none-win32.whl

You areusing pip version 7.0.1, however version 8.0.2 is available.

Youshould consider upgrading via the 'pip install --upgrade pip' command.

Pillow-3.1.0-cp34-none-win_amd64.whlis not a supported wheel on this platform.

使用命令pip install--upgrade pip更新PIP的版本:

 

更新完PIP版本,接下來就可以安裝PIL庫。

四、安裝PIL

PIP安裝完畢,安裝PIL庫就很容易了。執行下面的命令:

C:\Python27\Scripts>pip install Pillow-3.1.0-cp27-none-win32.whl

安裝完畢,需要檢查一下安裝是否成功。

如下圖所示,打開python後,輸入import Image,會提示沒有這個模塊。因爲使用pillow庫需要使用from PIL import Image代替import Image。按照圖中的處理,即可驗證PIL是否安裝成功。

 

 

PIL安裝成功後,接下來我們就可以使用python進行圖像處理了。

注:爲了方便大家搭建win7下的python+PIL環境,我將所用到的python2.7和PIL庫傳到了CSDN上。具體地址爲:

http://download.csdn.net/detail/icamera0/9425487

到此可能會報錯誤:更新失敗,原有的更新東西都被清除

  1. easy_install Pillow  
居然成功了


問題就這麼解決了!!! 

注:1,安裝的第三方模塊會保存在python根目錄下的\Lib\site-packages下 
     2,其實安裝模塊也可以直接對相應的模塊進行下載手動安裝,但不推薦
 

   


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章