2015-11-03 67 views
1

我有一個用Python編寫的腳本。作者決定使用僅在Python 3中可用的新功能,因此我必須更新我的版本。Python 2與Python 3的導入

現在我遇到了麻煩,因爲腳本在導入語句中崩潰,所以我決定做一些調試。我得出的結論是,我的Python 3無法從PIL導入Image

在Python 2:

Python 2.7.10 (default, Aug 22 2015, 20:33:39) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.1)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 
>>> from PIL import Image 

不給出一個錯誤,但在Python 3:

Python 3.5.0 (v3.5.0:374f501f4567, Sep 12 2015, 11:00:19) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 
>>> from PIL import Image 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
ImportError: No module named 'PIL' 

這究竟是爲什麼以及如何解決這個問題?

+0

PIL不是標準庫,您需要安裝它。我建議枕頭叉(更新PIL包裝和錯誤修復)。 –

+2

單獨安裝Python 2和Python 3的模塊。 Python 3不能「共享」你已經安裝在Python 2下的模塊。如果你開始使用Python 3,你需要安裝任何你想使用的庫的Python 3版本。 – BrenBarn

+1

您是否在您的Python 3安裝上安裝了枕頭? –

回答

4

PIL不是標準庫;安裝它的Python 2,但該安裝不是(並不能)使用被Python 3.

明確地在Python 3安裝PIL(或更確切地說,枕頭叉),以及:

python3 -m ensurepip # optional, makes sure pip is installed 
python3 -m pip install Pillow