Python 2与Python 3导入

时间:2015-11-03 19:23:18

标签: python python-2.7 python-import pillow python-3.5

我有一个用Python编写的脚本。作者决定使用仅在Python 3中提供的新功能,因此我不得不更新我的版本。

现在我遇到了麻烦,因为脚本在import语句中崩溃了,所以我决定做一些调试。我得出结论,我的Python 3无法从Image导入PIL

在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'

为什么会发生这种情况,我该如何解决?

1 个答案:

答案 0 :(得分:4)

PIL不是标准库;你为Python 2安装了它,但Python 3没有(也不能)使用它。

在Python 3中也明确安装PIL(或者更确切地说,Pillow fork):

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