【发布时间】:2016-02-04 02:12:19
【问题描述】:
我有一个用 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 不是标准库,需要安装。我推荐 Pillow fork(更新的 PIL 打包和错误修复)。
-
Python 2 和 Python 3 的模块是分开安装的。 Python 3 无法“共享”您已在 Python 2 下安装的模块。如果您开始使用 Python 3,则需要安装任何要使用的库的 Python 3 版本。
-
您是否在 Python 3 安装中安装了枕头?
标签: python python-2.7 python-import pillow python-3.5