[英]Python 2 vs Python 3 imports
我有一個用Python編寫的腳本。 作者決定使用僅在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'
為什么會發生這種情況,我該如何解決?
PIL不是標准庫; 您是為Python 2安裝的,但Python 3沒有(也不能)使用該安裝。
還要在Python 3中顯式安裝PIL(或更確切地說是Pillow分支):
python3 -m ensurepip # optional, makes sure pip is installed
python3 -m pip install Pillow
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.