Python 2 对比 Python 3 进口

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

不报错,但是在Python3:

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 fork):

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